Filter queryset in child inline based on selected parent
Assume I have these models:
class GrandParent(models.Model):
name = models.CharField(...)
class Parent(models.Model):
grand_parent = models.ForeignKey(GrandParent)
name = models.CharField(max_length=255)
class Child(models.Model):
parent = models.ForeignKey(Parent, related_name="children", on_delete=models.CASCADE)
name = models.CharField(...)
Now in admin:
class ChildInline(nested_admin.NestedTabularInline):
model = Child
class ParentInline(nested_admin.NestedTabularInline):
model = Parent
inlines = (ChildInline,)
@admin.register(GrandParent)
class GrandParentAdmin(nested_admin.NestedModelAdmin):
inlines = (ParentInline,)
Now here's my dilemma. When the admin selects a Parent in admin and saves, I want to restrict Children selection to those belonging to the Parent selected, not children of any other parent. Now you should also know that Parent is not actually the object being edited in the current page but it's also nested inline to another model, in this case GrandParent.
The Children inlines will automatically filter so that they only include those belonging to the Parent—though if you allow adding then it is possible for the user to add new ones. This is the standard inline behavior, not only for nested inlines but for regular inlines as well. Is it not working that way for you?
Closing, as this has been awaiting a response for a year. If the original reporter (or anyone else for that matter) can add context to the issue then please reopen it.