【发布时间】:2019-11-22 22:06:20
【问题描述】:
我有一个模型 Person 和另一个模型 Relation。 在创建新关系之前,我想检查这种关系是否可行。
这篇文章和其他一些类似的文章提供了一个解决方案,但对于自引用模型,我的模型不是自引用的。 Django self-recursive foreignkey filter query for all childs
class Person(models.Model):
identifier = IntegerField(null=True)
title = CharField(max_length=100, null=True)
def get_all_parent_ids(self):
# I want to get all the ancestors of this person
# but this method only gets immediate parents right now
return [parent.parent.id for parent in self.parenting.all()]
def get_all_children_ids(self):
# I want to get all the descendants of this person
# but this method only gets immediate children right now
return [child.children.id for child in self.baby_sitting.all()]
class Relation(models.Model):
name = CharField(max_length=50, null=True)
parent = ForeignKey(Person, on_delete=models.PROTECT, related_name="parenting")
children = ForeignKey(Person, on_delete=models.PROTECT, related_name="baby_sitting")
class Meta:
unique_together = ('parent', 'children')
def is_relation_possible(new_parent_id, new_children_id):
new_parent_person = Person.objects.get(pk=new_parent_id)
all_parents = new_parent_person.get_all_parent_ids()
if new_children_id in all_parents:
return False
else:
return True
例如:现有关系 - A到B - B到C - C 到 D - D 到 E
我希望 is_relation_possible(E, A) 返回 False,因为 E 有一个祖先 A。
目前它只检查直系父母,而不是所有父母。
【问题讨论】:
标签: django django-models recursive-query