Damon Brodie
Damon Brodie

Reputation: 309

related_name works the first time but not the second

I have a somewhat deep hierarchy of 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)

In my code I start creating individuals, their associated facts, and the fact_details. The related_name "fact_set" gets created automatically and works, but why doesn't this automatically work for factdetails too? should there not be a factdetail_set related name created?

                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,
                        )

When I call curr_individual.fact_set.whatever it works fine, but curr_individual.fact_set.factdetail_set.whatever fails.

Traceback (most recent call last): File "./manage.py", line 15, in execute_from_command_line(sys.argv) File "/Users/damon.brodie/git/theycamebeforeme/tcbm_python/lib/python3.7/site-packages/django/core/management/init.py", line 381, in execute_from_command_line utility.execute() File "/Users/damon.brodie/git/theycamebeforeme/tcbm_python/lib/python3.7/site-packages/django/core/management/init.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/damon.brodie/git/theycamebeforeme/tcbm_python/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv self.execute(*args, **cmd_options) File "/Users/damon.brodie/git/theycamebeforeme/tcbm_python/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute output = self.handle(*args, **options) File "/Users/damon.brodie/git/theycamebeforeme/server/tcbm/db/management/commands/validate_gedcoms.py", line 31, in handle parsed_gedcoms = GedcomManager.parse_all() File "/Users/damon.brodie/git/theycamebeforeme/server/tcbm/db/models.py", line 70, in parse_all if gedcom.check_parse(): File "/Users/damon.brodie/git/theycamebeforeme/server/tcbm/db/models.py", line 430, in check_parse self.read_gedcom() File "/Users/damon.brodie/git/theycamebeforeme/server/tcbm/db/models.py", line 228, in read_gedcom curr_individual.fact_set.factdetail_set.create( AttributeError: 'RelatedManager' object has no attribute 'factdetail_set'

Upvotes: 0

Views: 37

Answers (1)

art
art

Reputation: 1412

Your curr_individual is a single object and when you call curr_individual.fact_set it gives a set of facts related to that particular object. Now,in order to call factdetail_set, you need to do it from each single fact objects.

Upvotes: 1

Related Questions