I am trying to write some validations on form input and need to check that the instance being created doesn't already exist under the same grandparent. The field I am validating against isn't a primary key as it can exist outside the 'family'. I can't seem to think of an appropriate query for this but so far I have the following which works:
existing_parents = Parent.objects.filter(grandparent=active_parent.grandparent)
for parent in existing_parents:
existing_children = parent.children.all()
for children in existing_children
if existing_children.identifier == identifier:
self._errors["form_field"] = self.error_class(
["That child already exists"]
)
else:
pass
Just wondered if there was a lookup I can do that simplifies it?