Opened 6 years ago

Closed 6 years ago

#30110 closed Uncategorized (invalid)

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

Reported by: quindraco Owned by: nobody
Component: Database layer (models, ORM) Version: 2.1
Severity: Normal Keywords: datetimefield
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by quindraco)

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:

Change History (2)

in reply to:  description comment:1 by Nasir Hussain, 6 years ago

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

comment:2 by quindraco, 6 years ago

Description: modified (diff)
Resolution: invalid
Status: newclosed

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

Note: See TracTickets for help on using tickets.
Back to Top