django-nested-inline icon indicating copy to clipboard operation
django-nested-inline copied to clipboard

Library does not support Django's `get_inlines` method.

Open PedroPerpetua opened this issue 2 years ago • 0 comments
trafficstars

Django inlines traditionally use the method get_inlines to retrieve the list of inlines in a model, in case the user needs to conditionally decide which inlines to render.

Even though the Nested inlines inherit from Django's ModelAdmin, they do not appear to support this method. Specifically, the InlineInstancesMixin should change from self.inlines to self.get_inlines() in order to support this method.

Here's the work around I'm currently using:

class MyModelAdmin(NestedModelAdmin):
    def get_inline_instances(
        self, 
        request: HttpRequest, 
        instance: Optional[MyModel] = None
    ) -> Iterable[admin.options.InlineModelAdmin]:
        """Override this method to set the inlines before calling the get_inline_instances"""
        self.inlines = self.get_inlines(request, instance)
        return super().get_inline_instances(request, instance)

    def get_inlines(
        self, 
        request: HttpRequest,  
        instance: MyModel
    ) -> Iterable[admin.options.InlineModelAdmin]:
        # Custom logic that returns a list of Inlines to be rendered
        return list_of_inlines

PedroPerpetua avatar Aug 04 '23 14:08 PedroPerpetua