#22571: Document implications of using auto_now_add=True and get_or_create
-------------------------------------+-------------------------------------
     Reporter:  nu.everest@…         |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |                  Version:  1.4
    Component:  Documentation        |               Resolution:
     Severity:  Normal               |             Triage Stage:  Accepted
     Keywords:  integrityerror       |      Needs documentation:  0
  auto_now_add get_or_create         |  Patch needs improvement:  0
  duplicatekey                       |                    UI/UX:  0
    Has patch:  0                    |
  Needs tests:  0                    |
Easy pickings:  0                    |
-------------------------------------+-------------------------------------

Comment (by charettes):

 Replying to [comment:4 mardini]:
 > Note that your date_added is a DateTimeField, not a DateField, so it
 takes time into account. That's why using "today's date" won't match the
 existing value of date_added (since it's today's date + a particular
 time). If however some_datetime_obj in your first is exactly the same date
 and time, no new object will be created. If you don't want to consider the
 time in your get_or_create(), you can either use a DateField for
 date_added, or filter against year/month/day of your DateTimeField. So I
 think this ticket is invalid.

 I agree that the second example is a bit wrong but there's a legitimate
 issue to be documented here.

 {{{#!python
 from django.db import models
 from django.utils import timezone

 class Auto(models.Model):
     added = models.DateTimeField(auto_now_add=True)

 class Default(models.Model):
     added = models.DateTimeField(default=timezone.now)
 }}}

 {{{#!python
 In [1]: from app.models import Auto, Default, timezone

 In [2]: added = timezone.now()

 In [3]: Auto.objects.get_or_create(added=added)
 Out[3]: (<Auto: Auto object>, True)

 In [4]: Auto.objects.get_or_create(added=added)
 Out[4]: (<Auto: Auto object>, True)

 In [5]: Default.objects.get_or_create(added=added)
 Out[5]: (<Default: Default object>, True)

 In [6]: Default.objects.get_or_create(added=added)
 Out[6]: (<Default: Default object>, False)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22571#comment:5>
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 unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/078.020a102f1c9d2b6a9283c058275e5478%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to