Django many to many не симметричная реализация родителя. Обратный поиск возвращает ребенка, а не родителей
Hi,
In models.py I have this model:
class element (models.Model): element=models.Autofield(primary-key=true) parent_element=models.ManyToMayField('self',null=True,blank=True, related_name='parents',symmetrical=False)
>This relationship is implemented in database, by django, into a table with "from_element" (child, my element) and "to_element" (parents of my element) fields.
The problems happens when I try to obtain the parents of a element with a reverse lookup,giving as result the elements that have my element as parent, the childs.
Debugging the parents I see this:
element.parents.source_field_name='to_element' element.parents.target_field_name='from_element'
>instead of
element.parents.source_field_name='from_element' element.parents.target_field_name='to_element'
>
Giving an example, if I create the element 1, and after I create the element 2 with element 1 as "parent" this is what I have:
element.objects.get(element=1).parents.all() is 2 (his child, wrong)
element.objects.get(parent=1) is 2 (his child, rigth)
element.objects.get(element=2).parents.all() do not exist (wrong)
element.objects.get(parent=2) do not exist (right)
In database I have this: from_element is 2 and to_element is 1. But, as I said, parents.all() is looking from "to_element" to "from_element"
Many to one and one to many are not the same, they have a direction. It is easy to define a one-to-many when you are using different models, define the foreign key into the "many" model and it's done, but not in 'self' case. Looking for information I always found "use a Foreign Key" or "use Many to Many no symmetrical". But, in my case, an element could have multiple parents, parents could have multiple childs (so I can not use ForeignKey) and a parent is not child of his childs (symmetrical).
Итак, вопрос: как я могу определить это направление или взаимосвязь? Или, по крайней мере, как я могу сделать обратный поиск в нужном направлении? (от_элемента к_элементу)
Заранее большое спасибо и пожелания.