Re: Overriding update in models
On Tue, Nov 4, 2008 at 9:58 PM, Antonio Volpon <[EMAIL PROTECTED]>wrote: > > Hello. > > I am very new to Django and I'm having some problems in overriding the > save method for a simple class. In particular, the following code > (models.py) used against a Mysql database doesn't change the value of > the two date fields when i save the page in admin, while it correctly > changes the value of description. I think I'm doing something > completely wrong, right? > > Thanks a lot, > Antonio > > import datetime > from django.db import models > > class Object(models.Model): >description = models.CharField(max_length=255) >insert_date = models.DateTimeField(editable=False) >update_date = models.DateTimeField(editable=False) > >def save(self): >if not self.id: >self.insert_date = datetime.date.today() >self.update_time = datetime.date.today() >super(Object, self).save() > Hakan Waara is right, you can use auto_now. But for arbitrary operations on datetime filelds try datetime.datetime.now() and datetime.timedelta(), not datetime.date. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Overriding update in models
Thanks both. Horrible typo. I noticed, however, that the time update isn't the current one. Have to look at some locale? --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Overriding update in models
Instead of manually setting the update time and created date, as it seems like you mean to do here, I highly recommend auto_now and now auto_now_add attributes that you can add to your date fields, to get this functionality for free. See http://docs.djangoproject.com/en/dev/ref/models/fields/#datetimefield /Håkan 4 nov 2008 kl. 22.41 skrev Antonio Cavedoni: > > Ciao Antonio, > > On Nov 4, 2008, at 8:58 PM, Antonio Volpon wrote: >> import datetime >> from django.db import models >> >> class Object(models.Model): >> description = models.CharField(max_length=255) >> insert_date = models.DateTimeField(editable=False) >> update_date = models.DateTimeField(editable=False) >> >> def save(self): >> if not self.id: >> self.insert_date = datetime.date.today() >> self.update_time = datetime.date.today() >> super(Object, self).save() > > you have a field called update_date but then you reference it as > self.update_time in the save() method, maybe that’s why it isn’t > working? > > Cheers! > -- > Antonio > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Overriding update in models
Ciao Antonio, On Nov 4, 2008, at 8:58 PM, Antonio Volpon wrote: > import datetime > from django.db import models > > class Object(models.Model): >description = models.CharField(max_length=255) >insert_date = models.DateTimeField(editable=False) >update_date = models.DateTimeField(editable=False) > >def save(self): >if not self.id: >self.insert_date = datetime.date.today() >self.update_time = datetime.date.today() >super(Object, self).save() you have a field called update_date but then you reference it as self.update_time in the save() method, maybe that’s why it isn’t working? Cheers! -- Antonio --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Overriding update in models
Alex, thanks a lot for the suggestion, but it doesn't work. Antonio --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Overriding update in models
Hi, Antonio. Try to replace `datetime.date.today()` with `datetime.datetime.now()` calls. On Tue, Nov 4, 2008 at 23:58, Antonio Volpon <[EMAIL PROTECTED]>wrote: > > Hello. > > I am very new to Django and I'm having some problems in overriding the > save method for a simple class. In particular, the following code > (models.py) used against a Mysql database doesn't change the value of > the two date fields when i save the page in admin, while it correctly > changes the value of description. I think I'm doing something > completely wrong, right? > > Thanks a lot, > Antonio > > import datetime > from django.db import models > > class Object(models.Model): >description = models.CharField(max_length=255) >insert_date = models.DateTimeField(editable=False) >update_date = models.DateTimeField(editable=False) > >def save(self): >if not self.id: >self.insert_date = datetime.date.today() >self.update_time = datetime.date.today() >super(Object, self).save() > > > > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Overriding update in models
Hello. I am very new to Django and I'm having some problems in overriding the save method for a simple class. In particular, the following code (models.py) used against a Mysql database doesn't change the value of the two date fields when i save the page in admin, while it correctly changes the value of description. I think I'm doing something completely wrong, right? Thanks a lot, Antonio import datetime from django.db import models class Object(models.Model): description = models.CharField(max_length=255) insert_date = models.DateTimeField(editable=False) update_date = models.DateTimeField(editable=False) def save(self): if not self.id: self.insert_date = datetime.date.today() self.update_time = datetime.date.today() super(Object, self).save() --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---