Re: Set two fields to the same value

2012-07-21 Thread Melvyn Sopacua
On 12-7-2012 15:51, Jaroslav Dobrek wrote:

> Users may create Test objects in order to run their own tests. A test 
> always starts at some date time and it always ends at some date time. Each 
> test has a time which is increased until it equals the end time. When a 
> user creates a new test (and before he uses it) the field time should have 
> the same value as the field start_time.

(Assuming the time field should be stored in the database)

Simplest, but specific for temporal fields:
class Test(models.Model) :
start_time = models.DateTimeField(auto_now_add=True)
end_time = models.DateTimeField(auto_now_add=True)
time = models.DateTimeField(auto_now_add=True)

Generally working:
class Test(models.Model) :
first = models.CharField(max_length=10, default='first')
second = models.CharField(max_length=10)

def __init__(self, *args, **kwargs) :
first = kwargs.get('first', False)
if not first :
first = self.__class__.first.default
second = first
if not kwargs.get('second', False) :
kwargs['second'] = second
super(Test, self).__init__(*args, **kwargs)

For the admin, see

for an alternate solution.
-- 
Melvyn Sopacua


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Set two fields to the same value

2012-07-12 Thread luthur
you could only assign a Field variable in the Model, "time" absolutely not a 
Field, the error appears. 

-- 
luthur



On Thursday, July 12, 2012 at 9:51 PM, Jaroslav Dobrek wrote:

> Hello,
> 
> how can I design a model such that two fields are set to the same value when 
> a new object of the type defined by the model is created?
> 
> Example:
> 
> class Test(models.Model):
> start_time = models.DateTimeField()
> end_time = models.DateTimeField()
> time = start_time
> 
> 
> Users may create Test objects in order to run their own tests. A test always 
> starts at some date time and it always ends at some date time. Each test has 
> a time which is increased until it equals the end time. When a user creates a 
> new test (and before he uses it) the field time should have the same value as 
> the field start_time.
> 
> The code above will produce a database error.
> 
> Jaroslav
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/django-users/-/ekl9tv3zHTIJ.
> To post to this group, send email to django-users@googlegroups.com 
> (mailto:django-users@googlegroups.com).
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com 
> (mailto:django-users+unsubscr...@googlegroups.com).
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



RE: Set two fields to the same value

2012-07-12 Thread lacrymol...@gmail.com

You could override .save...

-Mensaje original-
De: Jaroslav Dobrek
Enviados:  12/07/2012 10:51:08
Asunto:  Set two fields to the same value

Hello,

how can I design a model such that two fields are set to the same value 
when a new object of the type defined by the model is created?

Example:

class Test(models.Model):
start_time = models.DateTimeField()
end_time = models.DateTimeField()
time = start_time

Users may create Test objects in order to run their own tests. A test 
always starts at some date time and it always ends at some date time. Each 
test has a time which is increased until it equals the end time. When a 
user creates a new test (and before he uses it) the field time should have 
the same value as the field start_time.

The code above will produce a database error.

Jaroslav

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/ekl9tv3zHTIJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Set two fields to the same value

2012-07-12 Thread Jaroslav Dobrek
Hello,

how can I design a model such that two fields are set to the same value 
when a new object of the type defined by the model is created?

Example:

class Test(models.Model):
start_time = models.DateTimeField()
end_time = models.DateTimeField()
time = start_time

Users may create Test objects in order to run their own tests. A test 
always starts at some date time and it always ends at some date time. Each 
test has a time which is increased until it equals the end time. When a 
user creates a new test (and before he uses it) the field time should have 
the same value as the field start_time.

The code above will produce a database error.

Jaroslav

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/ekl9tv3zHTIJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.