How to set for m2m-field different querysets for each inline object?

How to set for m2m-field different querysets for each inline object?



class SomeModel(models.Model):
    my_m2m_field = models.ManyToManyField(OtherModel)

class SomeModelInlineAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyInlineAdminForm, self).__init__(*args, **kwargs)

        self.fields['my_m2m_field'].queryset = OtherModel.objects.filter(somemodel__isnull=True)

class SomeModelInlineAdmin(admin.TabularInline):
    model = SomeModel
    form = SomeModelInlineAdminForm


Learn More :