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 = (
)