Django inline filter foreignkey

Django -- Posted on Aug. 24, 2017

django model admin filter foreign key in inline form

              
                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)
                  
   
            

Related Posts