[Django]-Concatenate a specific field from child objects into parent object?

3👍

You could do something like this:

chapter = Chapter.objects.get(chapter=1)  # or whatever chapter you want to get

content = ""

for verse in chapter.verse_set.all():
   content += f"{verse.verse} "

content = content.rstrip()

chapter.chapter_content = content
chapter.save()

But it is bad practice to store the verse both in Verse objects as in Chapter objects, because what if you want to update just one verse?

You could try to make the chapter_content a method property in the Chapter model.

class Chapter(models.Model):
    chapter = models.IntegerField()
    verses_amount = models.IntegerField()

    @property
    def chapter_content(self):
        content = ""
        for verse in self.verse_set.all():
            content += f"{verse.verse} "
        return content.rstrip()
    

class Verse(models.Model):
    chapter = models.ForeignKey(Chapter, on_delete=CASCADE)
    verse = models.TextField()
    verse_number = models.IntegerField()

Note that you still have to sort the verses by their order number.

👤Wouter

Leave a comment