﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
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	quindraco	nobody	"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:

"	Uncategorized	closed	Database layer (models, ORM)	2.1	Normal	invalid	datetimefield		Unreviewed	0	0	0	0	0	0
