[Answered ]-DjangoAdmin: Accesing the parent instance within an Inline admin

2👍

Well I found a similar question here in StackOverflow, and used the method described there to solve it.

It uses the parent_model attribute from inlines, and the resolve method from django.core.urlresolvers to get the instance based in the url.

Here’s the code:

    def get_object(self, request):
        resolved = resolve(request.path_info)
        if resolved.args:
             return self.parent_model.objects.get(pk=resolved.args[0])
        return None

Then I would call the get_object method inside of my formfield_from_foreignkey method to get the instance of the object I want to use as a filter.

Hope it helps!

Leave a comment