Custom changelist and list_editable in Django Admin

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

The problem I am facing is that Django requires all fields from to list_editable

be fields in the 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 field on the model name

, it doesn't work, no validation is performed.

Edit : The specific validation error I'm getting:

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

You can see this place in the sources: Django Sources .

+3


source to share


2 answers


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

def name(self):
   pass

      



makes it go away.

0


source


Adding some clarification for @kagronick's answer

Add to SomeModelAdmin



def name(self, obj=None):
    pass

      

0


source







All Articles