datetime field not saved

778 views
Skip to first unread message

bobhaugen

unread,
Jul 31, 2008, 6:39:47 AM7/31/08
to Django users
Using django-trunk revision 8160 (just updated a few minutes ago to
see if this was a recently fixed bug).

Database = sqlite3. Running on development server on Ubuntu 7.10.

Got 2 datetime fields in a model, like this:

proposed_time = models.DateTimeField(default=datetime.now())
accepted_time = models.DateTimeField(blank=True, null=True)

The default value for proposed_time works fine.

Can't get accepted_time to save, either from admin or views.
No errors, it just does not save the value - the value in the database
remains NULL.

Likewise if I try to change the value of proposed_time from admin, it
does not get saved.

Any clues?

Jarek Zgoda

unread,
Jul 31, 2008, 7:55:49 AM7/31/08
to django...@googlegroups.com
Wiadomość napisana w dniu 2008-07-31, o godz. 12:39, przez bobhaugen:

> Got 2 datetime fields in a model, like this:
>
> proposed_time = models.DateTimeField(default=datetime.now())
> accepted_time = models.DateTimeField(blank=True, null=True)
>
> The default value for proposed_time works fine.
>
> Can't get accepted_time to save, either from admin or views.
> No errors, it just does not save the value - the value in the database
> remains NULL.
>
> Likewise if I try to change the value of proposed_time from admin, it
> does not get saved.


You have been bitten by "modifiable default arguments" feature of
python - defaults are evaluated at module compilation time, not at
code execution. Use default=datetime.now (a callable, not value).

But I have no clue as to not-saving field value.

--
We read Knuth so you don't have to. - Tim Peters

Jarek Zgoda
jarek...@redefine.pl

Donn

unread,
Jul 31, 2008, 8:14:41 AM7/31/08
to django...@googlegroups.com
Also, what about the auto_now_add param? That could work:
pubdate = models.DateTimeField(auto_now_add=True)


\d

bobhaugen

unread,
Jul 31, 2008, 8:42:15 AM7/31/08
to Django users
On Jul 31, 6:55 am, Jarek Zgoda <jarek.zg...@redefine.pl> wrote:
> You have been bitten by "modifiable default arguments" feature of
> python - defaults are evaluated at module compilation time, not at
> code execution. Use default=datetime.now (a callable, not value).

Changed that, thanks for the tip. But that was the part that *did*
work, or at least saved a datetime value in the database.

> But I have no clue as to not-saving field value.

That's the problem I posted about, which is still unresolved, in case
anybody else has any ideas...

bobhaugen

unread,
Aug 5, 2008, 1:46:30 PM8/5/08
to Django users
Just in case somebody comes across this while searching for an answer
to a problem, the problem here was more than one stupid programmer
error. Nothing wrong with Django, DateTimeFields, field settings, or
sqlite3.

The stupid programmer errors included failing to specify
self.accepted_time in a model method, and a bug in an overriddent
save() method.
Reply all
Reply to author
Forward
0 new messages