[Fixed]-How to access class instance from within the inline declaration?

1👍

✅

Firstly, model should be Toppings.

class ToppingInlineAdmin(admin.TabularInline):
    model = Toppings

To conditionally set classes on your InlineModelAdmin you need to override a method that takes the object, django does not provide a method specifically for this functionality but I have overridden get_formset as an example

def get_formset(self, request, obj=None, **kwargs):
    if not obj or not obj.toppings.count():
        self.classes = ['collapse']
    return super(ToppingInlineAdmin, self).get_formset(request, obj=obj, **kwargs)

Leave a comment