[Answered ]-Django: How to remove one field from specific member of the admin inline

2πŸ‘

βœ…

It seems like Django calculates list of fields for formset at inline creation time and then formset insists on these fields to exist.

So the only ways to overcome this that I’ve found is to actually ignore saved value when needed:

class MemberAdminInlineForm(forms.ModelForm):
    def clean_last_name(self):
        if hasattr(self, "instance"):
            if self.instance.first_name == "Joe":
                return self.instance.last_name
        return self.cleaned_data["last_name"].

class MemberAdminInline(admin.TabularInline):
    model = Member
    fields = ("first_name", "last_name")
    readonly_fields = ("first_name", )
    form = MemberAdminInlineForm
πŸ‘€Zaar Hai

Leave a comment