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.'
CodePudding user response:
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.