[Django]-Get Foreign Key field name by which inline form is related

5👍

What exactly I guess , you are trying to get name of foreign key field related to particular modeladmin .

you should use save_formset fields in ModelAdmin , and find the foreign key name here.

all_formset_forms = formset.forms

This will return all inline forms of the current form you are trying to save.

Loop through each form fields and the fields you are asking for, i.e related foreign key field of a modeladmin instance is an attribute of parent_instance.

if hasattr(formset_form_field,'parent_instance'):
    formset_field_name  =  formset_form_field
    related_modeladmin_instance = formset_field_name.parent_instance

Leave a comment