title: How to have a different models.ForeignKey related_name for each linked objects of same model?
body: <p>I’m working with Django Rest to have multiple “text block” objects linked with the document object. I’ve accomplished this with a simple models.ForeignKey feature so far.</p>
  <p>However, I’m rendering all of these text blocks in multiple columns in the front end.
  The Textblock model will have a column field to determine which goes to which column. Since the order of these text blocks matter, I was afraid of having them all mixed together under single &quot;all_columns&quot; field
  So far, I figured the easiest way is to let DRF return something like the following:</p>
  <pre><code>{
      &quot;name&quot;: &quot;Comparing Two Characters&quot;,
      &quot;column_A&quot;: [
          {
              &quot;title&quot;: &quot;Text Block&quot;,
              &quot;body&quot;: &quot;lorem ipsum blah blah&quot;
              &quot;col&quot;: 1
          }
      ],
      &quot;column_B&quot;: [
          {
              &quot;title&quot;: &quot;Text Block 2&quot;,
              &quot;body&quot;: &quot;lorem ipsum blah blah&quot;
              &quot;col&quot;: 2
          },
              {
              &quot;title&quot;: &quot;Text Block 3&quot;,
              &quot;body&quot;: &quot;lorem ipsum blah blah&quot;
              &quot;col&quot;: 2
          }
      ]
  }
  </code></pre>
  <p>How would I be able to implement something like this? I’m not sure if using related fields is even ideal for such cases. I would appreciate any help!</p>
  <p>Here’s my current models.py code for reference:</p>
  <pre><code>class Document(models.Model):
      id = models.AutoField(primary_key=True)
      name = models.CharField(max_length=100)
      # other fields
      def __str__(self):
              return self.name
  
  class TextBlock(models.Model):
      id = models.AutoField(primary_key=True)
      document = models.ForeignKey(Document, related_name='blocks', on_delete=models.CASCADE)
      col = models.IntegerField()
      title = models.CharField(max_length=100)
      body = models.CharField(max_length=100)
  </code></pre>
  <h2>Edit:</h2>
  <p>What I'm getting returned with the updated code by sayeed910</p>
  <pre><code>            &quot;name&quot;: &quot;outlineblock-test&quot;,
              &quot;desc&quot;: &quot;&quot;,
              &quot;blocks&quot;: [
                  {
                      &quot;url&quot;: &quot;http://0.0.0.0:8000/api/v1/layoutblocks/7/&quot;,
                      &quot;col&quot;: 3,
                      &quot;title&quot;: &quot;col3&quot;,
                      &quot;placeholder&quot;: &quot;hehehe&quot;,
                      &quot;template&quot;: &quot;http://0.0.0.0:8000/api/v1/templates/3/&quot;
                  },
                  {
                      &quot;url&quot;: &quot;http://0.0.0.0:8000/api/v1/layoutblocks/6/&quot;,
                      &quot;col&quot;: 2,
                      &quot;title&quot;: &quot;col2&quot;,
                      &quot;placeholder&quot;: &quot;hehe&quot;,
                      &quot;template&quot;: &quot;http://0.0.0.0:8000/api/v1/templates/3/&quot;
                  },
                  {
                      &quot;url&quot;: &quot;http://0.0.0.0:8000/api/v1/layoutblocks/5/&quot;,
                      &quot;col&quot;: 1,
                      &quot;title&quot;: &quot;col1&quot;,
                      &quot;placeholder&quot;: &quot;haha&quot;,
                      &quot;template&quot;: &quot;http://0.0.0.0:8000/api/v1/templates/3/&quot;
                  }
              ],
  </code></pre>
  <p>serializers.py</p>
  <pre><code>class TextBlockSerializer(serializers.HyperlinkedModelSerializer):
     class Meta:
         model = TextBlock
         fields = '__all__'
  
  class DocumentSerializer(serializers.HyperlinkedModelSerializer):
      blocks = LayoutBlockSerializer(many=True, read_only=True)
      class Meta:
         model = Document
         fields = '__all__'
  </code></pre>