#16583: Explicitly passing a date to a DateTimeField in a model's create method
should override auto_now_add
-------------------------------------+-------------------------------------
               Reporter:  ian        |          Owner:  nobody
                   Type:  Bug        |         Status:  closed
              Milestone:             |      Component:  Database layer
                Version:  1.3        |  (models, ORM)
             Resolution:  invalid    |       Severity:  Normal
           Triage Stage:             |       Keywords:
  Unreviewed                         |      Has patch:  0
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
Changes (by aaugustin):

 * status:  new => closed
 * needs_better_patch:   => 0
 * resolution:   => invalid
 * needs_tests:   => 0
 * needs_docs:   => 0


Comment:

 Per the docs:
 > Automatically set the field to now when the object is first created.
 Useful for creation of timestamps. Note that '''the current date is always
 used; it's not just a default value that you can override.'''

 You can think of `auto_now_add` (and `auto_now`) as database triggers.

 In your situation, you shouldn't use `auto_now_add`. Instead you should
 override `save()` like this:
 {{{
 class MyModel(models.Model):
     def save(self, *args, **kwargs):
         if date_field is None:
             date_field = datetime.datetime.now()
         super(MyModel, self).save(*args, **kwargs)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/16583#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

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

Reply via email to