Django ModelAdmin - fieldsets ... field 'date' missing from the form

12,517

Solution 1

The error is due to date having auto_now_add=True (or auto_now=True).
As the value is automatic, it's not editable, so it's not in the form. To solve that, add this in FooAdmin:

readonly_fields = ("date",)

Solution 2

My problem was actually a bit different. My problem involved model inheritence and the django.contrib.admin User model.

This caused the problem:

from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.admin import UserAdmin
class AdminUser (UserAdmin):
    fieldsets = UserAdmin.fieldsets + (
        (_('APPS Info'), {'fields': ('agency', 'company')}),
    )

where "agency" and "company" are fields of my User model that extends django's user model. Your solution of putting those fields in readonly_fields did fix the error, but then those fields were read only, which isn't what I wanted. I found that the problem was that the ModelForm used in django.contrib.admin was setting the model to Django's user model. So to fix it I added this:

from django.contrib.auth.admin import UserAdmin, UserChangeForm as DjangoUserChangeForm
from django.utils.translation import ugettext_lazy as _
from apps_models.users.models import User
class UserChangeForm(DjangoUserChangeForm):
    class Meta:
        model = User
class AdminUser (UserAdmin):
    fieldsets = UserAdmin.fieldsets + (
        (_('APPS Info'), {'fields': ('agency', 'company')}),
    )
    form = UserChangeForm

That's what I get for using Model inheritance... it isn't pretty, but it got the job done.

So it sounds like we were getting the same error, but for different reasons.

Solution 3

It may also help some people to know the error also appears when there are incorrect fieldsets in the ModelAdmin, ...as when doing migrations and forgetting to delete removed fields....

Share:
12,517

Related videos on Youtube

Pascal Polleunus
Author by

Pascal Polleunus

Updated on November 15, 2021

Comments

  • Pascal Polleunus
    Pascal Polleunus over 2 years

    I figured out what the problem was while writing this question. I post it anyway in case it could help someone else.

    The error: 'FooAdmin.fieldsets[0][1]['fields']' refers to field 'date' that is missing from the form.

    With the following code:

    # models.py
    from django.db import models
    
    class Foo(Base):
        date = models.DateField(auto_now_add=True)
        title = models.CharField(max_length=255)
    
    # admin.py
    from django.contrib import admin
    
    class FooAdmin(BaseAdmin):
        list_display = ("title", "date")
        fieldsets = (
            (None, {
                "fields": ("date", "title")
            }),
        )
    
    admin.site.register(Foo, FooAdmin)
    
    • Matthew J Morrison
      Matthew J Morrison over 13 years
      Thanks for posting even though you solved your problem, I ran into this same issue earlier this week.
    • Dominic Rodger
      Dominic Rodger over 13 years
      can you remove the answer from the question body, and post it separately? Stack Overflow regulars (like me) will find it easier to know this question is answered, and also then we can upvote your answer. Thanks for contributing to the site!
  • rombarcz
    rombarcz about 13 years
    readonly_fields was introduced in django 1.2 - so it won't work in earlier versions. Thought it was worth mentioning.
  • Cerin
    Cerin about 13 years
    Great job on finding a simple solution despite Django's obnoxiously unintuitive error message.