Sorry for the long-winded explanation here, but this one is a Django data modeling doozy! Take however much time you need until you have the bandwidth to take a look at it; plenty else for me to work on in the meantime.
I've been working on this view to show all people related to a person via Person.relationships, an asymmetrical, self-referential, many-to-many through field. We want a list of each of a person's relationship objects from the intermediary table so we can display the relation type and notes. Listing person.relationships.all() only brings up a queryset of Person entries, so to get the list of relationship objects I use person.to_person.all().
However, person.to_person.all() does not in fact capture all related people. There is another set, person.from_person.all(), that may contain relationships to additional people, as well as some of the same people from the to_person set. The reasons for this are as follows:
Sometimes relationships are entered twice, i.e. once for each person in them. For example, in the section labeled "Related people (input manually)," Nahray b. Nissim has a relationship with Elḥanan with type "maternal cousin." Then on Elḥanan's page, there is another relationship entered for Nahray with type "paternal cousin". Thus, nahray.to_person.all() will capture just one of those two, the one entered manually from Nahray, and vice versa for elhanan.to_person.all().
However, sometimes relationships are only entered on one page. For example, Nahray b. Nissim has a relationship with Nissim b. Isḥāq with type "Parent". But Nissim b. Isḥāq doesn't have any entries input manually, so their ishaq.to_person.all() list is empty, failing to capture that relationship; it can only be accessed through ishaq.from_person.all().
On the admin page, we get around this by displaying both lists, with a read-only list from_person.all() labeled "Related people (automatically populated)". When the relationships are entered twice a la Nahray-Elḥanan, that means they actually appear twice on the admin page. But we don't want that on this people list page; we only want each one once, we just want to make sure the full set of related people is captured.
My current implementation ignores from_person and only displays the list from to_person.all(), i.e. input manually.
Is there a way to achieve showing both using the Django ORM? Here's what I had started to do, but it still results in duplicates. Does it seem like it's on the right track and just needs deduplication?
Hi @rlskoeser,
Sorry for the long-winded explanation here, but this one is a Django data modeling doozy! Take however much time you need until you have the bandwidth to take a look at it; plenty else for me to work on in the meantime.
I've been working on this view to show all people related to a person via
Person.relationships
, an asymmetrical, self-referential, many-to-many through field. We want a list of each of a person's relationship objects from the intermediary table so we can display the relation type and notes. Listingperson.relationships.all()
only brings up a queryset ofPerson
entries, so to get the list of relationship objects I useperson.to_person.all()
.However,
person.to_person.all()
does not in fact capture all related people. There is another set,person.from_person.all()
, that may contain relationships to additional people, as well as some of the same people from theto_person
set. The reasons for this are as follows:nahray.to_person.all()
will capture just one of those two, the one entered manually from Nahray, and vice versa forelhanan.to_person.all()
.ishaq.to_person.all()
list is empty, failing to capture that relationship; it can only be accessed throughishaq.from_person.all()
.On the admin page, we get around this by displaying both lists, with a read-only list
from_person.all()
labeled "Related people (automatically populated)". When the relationships are entered twice a la Nahray-Elḥanan, that means they actually appear twice on the admin page. But we don't want that on this people list page; we only want each one once, we just want to make sure the full set of related people is captured.My current implementation ignores
from_person
and only displays the list fromto_person.all()
, i.e. input manually.Is there a way to achieve showing both using the Django ORM? Here's what I had started to do, but it still results in duplicates. Does it seem like it's on the right track and just needs deduplication?
Maybe a simpler option would be some kind of solr index to achieve what I actually need with more flexibility?
Or does this in fact need to be a DB constraint that I overlooked, preventing the relations from being essentially entered twice?