#30110: Specifying the primary key on an object with a datetimefield that has a
callable default results in the datetime parser trying to parse the
default, not the called default
-------------------------------------+-------------------------------------
               Reporter:  quindraco  |          Owner:  nobody
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  2.1
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  datetimefield
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Assuming everything else is set up for your app, and an empty table for
 the model, this:
 {{{
 from django.db import models
 from django.utils import timezone

 class foo(models.Model):
   bar = models.DateTimeField(default=timezone.now)
 f = foo(id=1)
 f.save()
 }}}

 Will result in this:

 {{{
 TypeError: expected string or bytes-like object
 }}}

 This is thrown in django/utils/dateparse.py, in parse_datetime, at
 {{{
 match = datetime_re.match(value)
 }}}

 I did some digging, and the value is the default callable (here, the
 timezone.now function).  If you do the same thing without specifying the
 primary key, it works as expected (calling the callable).  Whether or not
 you specify a value for the datetimefield in question does not appear to
 matter.  I have not tested with other field types.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30110>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.3490ac8c49e08f192ae97b535698838c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to