from django.contrib import admin class InlineWithLimitedManyToManyFields(admin.StackedInline): limited_manytomany_fields = {} def get_formset(self, request, obj=None, **kwargs): # Hack! Hook parent obj just in time to use in formfield_for_manytomany self.parent_obj = obj return super(InlineWithLimitedManyToManyFields, self).get_formset( request, obj, **kwargs) def formfield_for_manytomany(self, db_field, request, **kwargs): if db_field.name in self.limited_manytomany_fields.keys() and \ hasattr(self,'parent_obj'): kwargs['queryset'] = db_field.rel.to.objects.filter(**{ self.limited_manytomany_fields[db_field.name]: self.parent_obj }) return super( InlineWithLimitedManyToManyFields, self).formfield_for_manytomany( db_field, request, **kwargs) class ExampleInline(InlineWithLimitedManyToManyFields): model = ExampleModel limited_manytomany_fields = { 'first_many_to_many_field_name': 'field_name_for_foreignkey_in_related_model_to_parent', 'second_many_to_many_field_name': 'field_name_for_foreignkey_in_related_model_to_parent', }