[Django] #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

9 views
Skip to first unread message

Django

unread,
Jan 17, 2019, 12:54:40 PM1/17/19
to django-...@googlegroups.com
#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.

Django

unread,
Jan 17, 2019, 2:12:11 PM1/17/19
to django-...@googlegroups.com
#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: Uncategorized | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: datetimefield | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Nasir Hussain):

Replying to [ticket:30110 quindraco]:
Hi, which python and database are you using? I tried with python 3.6.7,
django 2.1 and postgress and It worked fine for me.

--
Ticket URL: <https://code.djangoproject.com/ticket/30110#comment:1>

Django

unread,
Jan 17, 2019, 2:51:42 PM1/17/19
to django-...@googlegroups.com
#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: Uncategorized | Status: closed

Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: datetimefield | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by quindraco):

* status: new => closed
* resolution: => invalid


Old description:

> 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.

New description:

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.

Versions:

--

Comment:

....ah. I was using an out of date version. I'll set this bug to
invalid. Sorry about that. Could have sworn I checked that.

--
Ticket URL: <https://code.djangoproject.com/ticket/30110#comment:2>

Reply all
Reply to author
Forward
0 new messages