Can CreateView show additional rows for every row in a certain FK?

I'm sorry I do not know the correct terminology to describe what I am trying to accomplish briefly, so I am hoping you can read my explanation below. I have even added an image showing the results I want to accomplish.

Background + Explanation

I'm refactoring a language learning application in preparation to make it Open Source. It focuses on writing and composition. The way it works is that a user can create a Post and that post's text will automatically split into individual sentences via NLP where then each individual sentence will get added to another table called PostRow. In a separate view, a user on the platform is able to make corrections to this post. Then all of the individual corrections get added to the CorrectedRows table.

When a user visits the MakeCorrection view to correct a post then the form would essentially do this:

  1. Filter all of the PostRows for the Post in question
  2. For every filtered PostRow row show the CorrectedRow form too

In other words, if the Post's text has 6 sentences, then the MakeCorrection view should show 6 CorrectedRows.

enter image description here


class PostRow(SoftDeleteModel, TimeStampedModel):
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    sentence = models.TextField()

class CorrectedRows(SoftDeleteModel, TimeStampedModel):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    post_row = models.ForeignKey(PostRow, on_delete=models.CASCADE, default=None)
    correction = models.TextField()
    type = models.CharField(choices=CORRECTION_TYPE_CHOICES, max_length=3, blank=True)
    is_perfect_sentence = models.BooleanField(default=False)

class MakeCorrectionView(LoginRequiredMixin, CreateView, SuccessMessageMixin):
    model = CorrectedRows
    success_message = _("Corrections successfully published")

    # do something here to show multiple CorrectedRows depending on the number of PostRows

Thank you for the help in advanced! What would the correct terminology for this be called? I would love to read more about it!

Back to Top