[Answered ]-Django – Add classes to inline only if qs len is more than 5

1👍

There’s no "proper" way to do so, but… To achieve what you want you can pick any method that accepts obj argument. Like has_view_permission or get_forms or get_formset or get_extra

Note that you will not receive a QuerySet of the related manager, but instead will get the obj that is requested in the non-inline admin.

Example:


class TaskInline(admin.TabularInline):
    model = Task

    def has_view_permission(self, request, obj=None, **kwargs):
        # None for new instances or non object-specific calls
        if obj is not None:  
            # print(obj) -> instance that has Task as inline
            # TODO: fix the following line, because your field might be called differently
            if obj.tasks.count() >= 5:  
                self.classes = list(type(self).classes or []) + ["collapse"]
        return super().has_view_permission(request, obj=obj, **kwargs)

👤Art

Leave a comment