related_name第一次有效,但第二次无效

时间:2019-01-12 22:05:29

标签: django django-models

我的模型层次结构比较深:

models.py:

class Individual(models.Model):
    gedcom = models.ForeignKey(Gedcom, on_delete=models.CASCADE)

    pointer = models.CharField(max_length=22, default="")
    given = models.CharField(max_length=255, default="")
    surname = models.CharField(max_length=255, default="")
    birth_date = models.CharField(max_length=255, default="")
    birth_location = models.CharField(max_length=255, default="")
    death_date = models.CharField(max_length=255, default="")
    death_location = models.CharField(max_length=255, default="")

class Fact(models.Model):
    individual = models.ForeignKey(Individual, on_delete=models.CASCADE, blank=True)

    tag = models.CharField(max_length=4, default="")
    value = models.CharField(max_length=255, default="")
    priority = models.IntegerField(default=0)

class FactDetail(models.Model):
    fact = models.ForeignKey(Fact, on_delete=models.CASCADE, blank=True)

    tag = models.CharField(max_length=4, default="")
    value = models.CharField(max_length=255, default="")
    priority = models.IntegerField(default=0)

在我的代码中,我开始创建个人,与他们相关的事实以及fact_details。 related_name“ fact_set”是自动创建并起作用的,但是为什么它也不能自动用于事实细节呢?应该没有创建与factdetail_set相关的名称吗?

                curr_individual = self.individual_set.create(
                    pointer = curr_pointer,
                    given = given,
                    surname = surname,
                )
                elements = record.get_child_elements()
                for element in elements:
                    fact_details = element.get_child_elements()
                    fact_priority = 0
                    curr_fact_tag = element.get_tag()
                    curr_individual.fact_set.create(
                        tag = curr_fact_tag,
                        value = element.get_value(),
                        priority = fact_priority,
                    )
                    fact_priority += 1
                    fact_detail_priority = 0
                    for fact_detail in fact_details:
                        curr_fact_detail_tag = fact_detail.get_tag()
                        curr_fact_detail_value = fact_detail.get_value()
                        if not done_birth and curr_fact_tag == 'BIRT':
                            done_birth = True
                            if curr_fact_detail_tag == 'DATE':
                                curr_individual.birth_date = curr_fact_detail_value
                            if curr_fact_detail_tag == 'PLAC':
                                curr_individual.birth_location = curr_fact_detail_value
                        if not done_death and curr_fact_tag == 'DEAT':
                            done_death = True
                            if curr_fact_detail_tag == 'DATE':
                                curr_individual.death_date = curr_fact_detail_value
                            if curr_fact_detail_tag == 'PLAC':
                                curr_individual.death_location = curr_fact_detail_value
                        curr_individual.fact_set.factdetail_set.create(
                            tag = curr_fact_detail_tag,
                            value = curr_fact_detail_value,
                            priority = fact_detail_priority,
                        )

当我调用curr_individual.fact_set。时,一切正常,但是curr_individual.fact_set.factdetail_set。失败。

回溯(最近通话最近):   在第15行的文件“ ./manage.py”中     execute_from_command_line(sys.argv)   文件“ /Users/damon.brodie/git/theycamebeforeme/tcbm_python/lib/python3.7/site-packages/django/core/management/init.py”,行381,位于execute_from_command_line     utility.execute()   在执行的文件“ /Users/damon.brodie/git/theycamebeforeme/tcbm_python/lib/python3.7/site-packages/django/core/management/init.py”中执行     self.fetch_command(子命令).run_from_argv(self.argv)   在run_from_argv中的第316行中,文件“ /Users/damon.brodie/git/theycamebeforeme/tcbm_python/lib/python3.7/site-packages/django/core/management/base.py”     self.execute(* args,** cmd_options)   执行中的文件“ /Users/damon.brodie/git/theycamebeforeme/tcbm_python/lib/python3.7/site-packages/django/core/management/base.py”     输出= self.handle(* args,** options)   句柄中的文件“ /Users/damon.brodie/git/theycamebeforeme/server/tcbm/db/management/commands/validate_gedcoms.py”,第31行     parsed_gedcoms = GedcomManager.parse_all()   在parse_all的第70行,文件“ /Users/damon.brodie/git/theycamebeforeme/server/tcbm/db/models.py”     如果gedcom.check_parse():   文件“ /Users/damon.brodie/git/theycamebeforeme/server/tcbm/db/models.py”,行430,在check_parse中     self.read_gedcom()   在read_gedcom中的文件“ /Users/damon.brodie/git/theycamebeforeme/server/tcbm/db/models.py”中,第228行     curr_individual.fact_set.factdetail_set.create( AttributeError:“ RelatedManager”对象没有属性“ factdetail_set”

1 个答案:

答案 0 :(得分:1)

您的curr_individual是单个对象,当您调用curr_individual.fact_set时,它会提供与该特定对象相关的一组事实。现在,要调用factdetail_set,需要从每个事实对象中进行操作。