4

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.

Community
  • 1
  • 1
Andrey
  • 57,904
  • 11
  • 115
  • 158
  • What validation error do you get? Is `name` in `list_display` and not in `list_display_links` (either explicitly or by default)? – Mike Covington Jun 17 '15 at 17:11
  • @mfcovington I added to question for visibility – Andrey Jun 17 '15 at 17:18
  • Your question seems to be covered by Peter's answer to the other question. As he says, if the only field you want to edit is not a model field, then it gets complicated. He then gives a couple of suggestions. – Alasdair Jun 17 '15 at 17:20
  • @Alasdair I couldn't make any of them work. If you can provide specifics in an answer I would gladly accept it. I can't even understand how can it work because in Django sources there is very straightforward check for this case that fails. – Andrey Jun 17 '15 at 17:24
  • In addition to adding a method as Schuyler answered, the key is to NOT put `name` in `list_editable`. In Peter's answer in the linked question, he mentions that there must be at least one field in the list_editable. That is true, however, to make the custom field you describe work it must NOT be in `list_editable`. – JessieinAg May 08 '20 at 17:31

2 Answers2

1

Adding some clarification to answer by @kagronick

Add to SomeModelAdmin

def name(self, obj=None):
    pass
Schuyler
  • 71
  • 3
0

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.

kagronick
  • 2,332
  • 1
  • 20
  • 28