RadiantHex
RadiantHex

Reputation: 25567

Having a field named 'created' within a model - Django

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 have forgotten?

Upvotes: 2

Views: 155

Answers (3)

armnov
armnov

Reputation: 659

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)

Upvotes: 0

Manoj Govindan
Manoj Govindan

Reputation: 74755

Strange. I tried out your example and it worked perfectly well (Django 1.2.1, Python 2.6.2) Can you verify that:

  1. The field exists in the database (fire a SQL query perhaps)
  2. Check your admin.py (again) for any differences.

Update

@Daniel's answer is more likely to help the OP.

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599778

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.

Upvotes: 5

Related Questions