Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
Concatenate a specific field from child objects into parent object?

I have two models:

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

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

On Verse, I store a paragraph in the verse field. How can I query all children (Verse), get their verse paragraph, concatenate all those and put that in Chapter.chapter_content?

Eg

verses = Verse.objects.filter()[0:2]
for verse in verses:
    print(verse.verse)

>> 'Lorem Ipsum.'
>> 'Second Lorem Ipsum Iteration 2.'

In the parent Chapter, I want chapter_content to hold the two values as one long string:

'Lorem Ipsum. Second Lorem Ipsum Iteration 2.'

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!