Opened 6 years ago
Last modified 6 years ago
#30110 closed Uncategorized
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 — at Initial Version
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
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.