0

I am trying to join two serializers. I am trying to get table_db, otherwise, I am trying to insert in which register of the object (from table_db query), the list of other_table with table_db id.

The relationship is many to one, so, one other_table has one table_db, but, table_db have many other_table. It is clearer in the example:

     t_db =table_db.objects.all()
     data_table_db = table_dbSerializer(t_db,many=True).data

    for t in t_db:        
        o_t_db = other_table.objects.filter(table_db_id=t.pk)
        data_other_table= other_tableSerializer(o_t_db,many=True).data
        **data = data_table_db + data_other_table ????? //HOW CAN INSERT IT?**

    return Response(data, status=status.HTTP_200_OK)

Models

  class other_table(models.Model):
      table_db = models.ForeignKey(Table_db, on_delete=models.CASCADE, blank=True,null=True)

In table_db I do not have any reference to other_table, because it have many.

The serializers are basic for now:

from table_db.models import table_db
from other_table.models import other_table

class other_tableSerializer(serializers.ModelSerializer):
    class Meta:
       model = other_table
       fields = (
     )

class table_dbSerializer(serializers.ModelSerializer):
    class Meta:
       model = table_db
       fields = (
    )
Miguel Herreros Cejas
  • 664
  • 1
  • 12
  • 28

1 Answers1

1

Your code sample little bit ambiguous (to me). So, I decided to create a generic one for you. Also, I've made one simple change in the model, added the related_name argument in the FK field

# models.py
class Foo(models.Model):
    # some fields
    ...


class Bar(models.Model):
    foo = models.ForeignKey(Foo, on_delete=models.CASCADE, blank=True, null=True, related_name='bars')

Then I've created a Nested DRF Serializer as below,

# serializers.py
class BarSerializer(serializers.ModelSerializer):
    class Meta:
        model = Bar
        fields = '__all__'


class FooSerializer(serializers.ModelSerializer):
    bars = BarSerializer(many=True, read_only=True) # here is the magic happens

    class Meta:
        model = Foo
        fields = '__all__'

Then, simply pass the Foo queryset to the FooSerializer, you'll get the desired response.

# views.py
def foo_view(request):
    foo_qs = Foo.objects.all()
    foo_serializer = FooSerializer(foo_qs, many=True)
    return Response(foo_serializer.data)

References
1. What is related_name used for in Django?
2. FK related_name--Django Doc
3. DRF Nested Serializer

JPG
  • 82,442
  • 19
  • 127
  • 206
  • Thanks for your answer. The idea is just like your, and this code. (https://www.django-rest-framework.org/api-guide/relations/#nested-relationships) I tried it, but foo_serializer.data don't send bars in JSON response. I tried to add to query (object.all()) prefetch_related('bars') and select_related('bars'), but it produce an error. – Miguel Herreros Cejas Apr 28 '19 at 06:18
  • Wow!! It worked magic. I had a mistake with¡related_name and I put the wrong word. Now, it work correctly. Many Thanks!! – Miguel Herreros Cejas Apr 28 '19 at 06:28