django-nested-admin icon indicating copy to clipboard operation
django-nested-admin copied to clipboard

Filter queryset in child inline based on selected parent

Open realmhamdy opened this issue 5 years ago • 1 comments

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.

realmhamdy avatar Oct 14 '20 21:10 realmhamdy

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?

fdintino avatar Nov 16 '20 09:11 fdintino

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.

fdintino avatar Nov 14 '22 03:11 fdintino