简体   繁体   中英

Custom changelist_form and list_editable in Django Admin

I wanted to introduce custom editable field in Django Admin list view. I wanted to do something very similar to this question .

The problem that I am facing is that Django requires that all fields from list_editable must be fields in a model.

class SomeModel(Model):
   ...

class SomeModelChangeListForm(forms.ModelForm):
    class Meta:
        model = SomeModel
    name = forms.CharField()

class SomeModelAdmin(admin.ModelAdmin):
    def get_changelist_form(self, request, **kwargs):
        return SomeModelForm

    list_display = ['name']
    list_editable = ['name']

So if there is no name field on model it doesn't work, validation fails.

Edit : The specific validation error I am getting:

django.core.exceptions.ImproperlyConfigured: 'SomeModelAdmin.list_editable' refers to a field, 'name', not defined on SomeModel.

You can see this place in sources: Django sources .

Adding some clarification to answer by @kagronick

Add to SomeModelAdmin

def name(self, obj=None):
    pass

I ran into this the other day. It turns out simply having a variable or a method like

def name(self):
   pass

makes this go away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM