Я использую запросы вида «все потомки узла X с восходящим именем Y».
Оказывается, что Django возвращает разные результаты в зависимости от того, какой из родителей целевого узла указан как Y.
Иллюстрация: рассмотрим простую модель
class Person(models.Model):
name = models.CharField(max_length=200, unique=True, blank=False)
descendants = models.ManyToManyField("self",
symmetrical=False,
related_name='ascendants')
def __str__(self):
return self.name
Создайте граф с 3 узлами:
mother = Person(name="Mother")
mother.save()
son = mother.descendants.create(name="Son")
father = son.ascendants.create(name="Father")
Таким образом, «Отец» и «Мать» являются потомками «Сына».
Это загадочная часть. Я хочу вернуть сыновей, которые были у «Матери» с «Отцом». Однако код
q1 = mother.descendants.filter(ascendants__name="Father")
print("mother descendants with an ascendant called Father = ", q1)
создает пустой QuerySet, который кажется неправильным. В отличие от
q2 = mother.descendants.filter(ascendants__name="Mother")
print("mother descendants with an ascendant called Mother = ", q2)
правильно выдает <QuerySet [<Person: Son>]>
. Это ошибка Django?
Спасибо!