I have a model such as the following:
class Item(models.Model):
name = models.CharField(max_length=150)
created = models.DateTimeField(auto_now_add=True)
the admin class is the following:
class ItemAdmin(admin.ModelAdmin):
list_display = ('name', 'created')
the created field does not seem to exist
Is there some basic Django knowledge that I am missing or h开发者_如何学Pythonave forgotten?
When you say the field does not exist, do you mean that it is not showing on the admin change form? This is expected behaviour when using auto_now_add
. If you want the field to get a default value on creation but still be editable, use default=datetime.datetime.now
instead.
Strange. I tried out your example and it worked perfectly well (Django 1.2.1, Python 2.6.2) Can you verify that:
- The field exists in the database (fire a SQL query perhaps)
- Check your admin.py (again) for any differences.
Update
@Daniel's answer is more likely to help the OP.
Just make sure not to forget registering the ItemAdmin in admin.py:
admin.site.register(Item, ItemAdmin)
However, the 'created' field would only be displayed in the Item's list page, as well as if you add an additional field such as:
updated = models.DateTimeField(auto_now=True)
精彩评论