Jason Howard
Jason Howard

Reputation: 1586

Model field missing in admin

The invoice_date field on one of my models is not showing up in my admin.

invoice_date = models.DateField(auto_now=True, auto_now_add=False, null=True, blank=True)

I've tried the following:

I can successfully write to the field and pull data from it, so it appears to be there and functioning as desired. I just can't see it in the admin.

I have no special code in my admin.py. I've just registered the model

admin.py

from userorders.models import UserCartItem
admin.site.register(UserCart)

Any suggestions are welcome! Thanks!

Upvotes: 0

Views: 142

Answers (2)

Bernhard Vallant
Bernhard Vallant

Reputation: 50776

Accoring to Django's documentation:

As currently implemented, setting auto_now or auto_now_add to True will cause the field to have editable=False and blank=True set.

You can circumvent this by explicitly defining it on the ModelAdmin class:

from userorders.models import UserCartItem

class UserCartItemAdmin(admin.ModelAdmin):
    list_display = ['invoice_date']
    fields = ['invoice_date']
    # if you want the field just to visible but not editable
    # readonly_fields = ['invoice_date']


admin.site.register(UserCartItem, UserCartItemAdmin)

Upvotes: 1

The Coder
The Coder

Reputation: 616

You could try something following-

from userorders.models import UserCartItem

class UserCartItemAdmin(admin.ModelAdmin):
   list_display = ['field_name_1', 'field_name_2', 'invoice_date']

admin.site.register(UserCartItem, UserCartItemAdmin)

Upvotes: 0

Related Questions