[Django]-Django admin form : dynamic fields list causes a KeyError which I can bypass just by pressing F5, why?

4👍

✅

Here is working code:

class BlogPostAdmin(admin.ModelAdmin):

    def get_form(self, request, obj=None, **kwargs):
        if obj:
            if obj.kind == 'featured':
                self.exclude = ('attribut2',)
            elif obj.kind == 'classic':
                self.exclude = ('attribut1',)
        else:
            self.exclude = None

        return super(BlogPostAdmin, self).get_form(request, obj, **kwargs)

admin.site.register(Post, BlogPostAdmin)

I get the gist of why it works, but I’m not going to pretend to be smart enough to understand it fully. Maybe somebody can comment and explain why it works.

Leave a comment