[Django]-Django โ€“ 'extra_fields' in CustomModelForm is giving 'Unable to lookuup' error in models inline interface when trying to load that form

7๐Ÿ‘

โœ…

I found the solution when speculating this post. One should define the label field in the custom field like the following to avoid such error AttributeError: Unable to lookup 'extra_field' on Alternative or AlternativeInline.

class AlternativeForm(forms.ModelForm):

    extra_field = forms.BooleanField(label='is_answer', required=False)

    def save(self, commit=True):
        # extra_field = self.cleaned_data.get('extra_field', None)
        # ...do something with extra_field here...
        return super(AlternativeForm, self).save(commit=commit)

0๐Ÿ‘

To have the extra fields in the admin:

class YourModelAdmin(admin.ModelAdmin):

    form = YourModelForm

    fieldsets = (
        (None, {
            'fields': ('other fields here', 'extra_field',),
        }),
    )

Leave a comment