dkoug Blog

Django inline filter foreignkey

Django -- Posted on Aug. 24, 2017

django model admin filter foreign key in inline form

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "field":
            try:
                parent_obj_id = request.resolver_match.args[0]
                kwargs["queryset"] = Model.objects.exclude(
                    pk=parent_obj_id)
            except IndexError:
                pass
        return super(Inline, self).formfield_for_foreignkey(
            db_field, request, **kwargs)