DateTimeField's 'auto_now_add' only set once with 'edit_inline'

111 views
Skip to first unread message

Berco Beute

unread,
Feb 29, 2008, 6:02:00 AM2/29/08
to Django users
Sorry if I'm asking the obvious, but I can't figure this one out. With
the models below, creating a new Poll object with several Choices is
no problem, all from one admin page. Each Choice object is created
with the automatically set 'creationDate'. But when I try to edit that
Poll object I get an error when saving saying 'creationDate' cannot be
null. It seems as if Django doesn't accept that the field has already
been set before and doesn't need to be set again. Adding 'blank=True'
or 'core=True' doesn't help either.

2B

===========================
class Poll(models.Model):
name = models.CharField(max_length=200)

class Admin:
pass

class Choice(models.Model):
poll = models.ForeignKey(Poll, edit_inline=models.TABULAR,
num_in_admin=10)
creationDate = models.DateTimeField(auto_now_add=True)

Karen Tracey

unread,
Feb 29, 2008, 11:11:42 PM2/29/08
to django...@googlegroups.com

I believe this is a very old problem, see:

http://code.djangoproject.com/ticket/1030

Instead of using auto_now_add, try setting default=datetime.now.  I think that will accomplish the same thing and work properly when you edit the objects.

Karen

Berco Beute

unread,
Mar 1, 2008, 8:20:56 AM3/1/08
to Django users
Thanks, that is indeed one step closer to what I want. The problem
that now arises is that this way the field shows up in the admin
interface. I don't want users to fill it out, the default value is
fine.

Evert Rol

unread,
Mar 1, 2008, 11:46:41 AM3/1/08
to django...@googlegroups.com
> Thanks, that is indeed one step closer to what I want. The problem
> that now arises is that this way the field shows up in the admin
> interface. I don't want users to fill it out, the default value is
> fine.

Can you do something with the 'fields' attribute in an inner Admin
class?

Berco Beute

unread,
Mar 1, 2008, 3:47:09 PM3/1/08
to Django users
> Can you do something with the 'fields' attribute in an inner Admin
> class?

Adding an inner Admin class means that the object will be editable
through the admin interface, which is not what I want. I want the
object to be edited inline.

2B

Evert Rol

unread,
Mar 1, 2008, 4:14:21 PM3/1/08
to django...@googlegroups.com

Sorry, but then I'm misunderstanding your problem:

" The problem that now arises is that this way the field shows up in
the admin
> interface. I don't want users to fill it out, the default value is
> fine. "

Using the fields attribute, you should be able to prevent the
creationDate from showing up in the admin (letting it have its default
value), which seems to be what you want. The poll object can still be
modified inline from the Choice admin section.
I have assumed here, btw, that with 'users' you mean 'administrators',
with abilities to edit part of the admin section (ie, not superusers).

Berco Beute

unread,
Mar 1, 2008, 4:30:28 PM3/1/08
to Django users

> Using the fields attribute, you should be able to prevent the
> creationDate from showing up in the admin (letting it have its default
> value), which seems to be what you want. The poll object can still be
> modified inline from the Choice admin section.
> I have assumed here, btw, that with 'users' you mean 'administrators',
> with abilities to edit part of the admin section (ie, not superusers).

Thanks.
Yes, using the 'fields' attribute in an Admin inner class indeed means
I can show/hide any of the fields. The only problem I have with the
inner Admin class is that through the admin interface the Choice
object is now seperately editable as well (as oppossed to solely
inline when editing a Poll object). I would rather not have that.

2B
Reply all
Reply to author
Forward
0 new messages