I have 3 models T, E, Q such that:
class E(Model):
name_e = CharField()
class T(Model):
name_t = CharField()
e = ForeignKey(E)
class Q(Model):
name_q = CharField()
e = ForeignKey(E)
I also have Serializers for each:
class ESerializer(ModelSerializer):
class Meta:
model = E
fields = '__all__'
class TSerializer(ModelSerializer):
e = ESerializer()
class Meta:
model = T
fields = '__all__'
class QSerializer(ModelSerializer):
e = ESerializer()
class Meta:
model = Q
fields = '__all__'
For one E row there can be many T and Q rows. I can make a query such that
t = T.objects.filter(e__id=1).all()
serialized_t = TSerializer(t, many=True).data
And the output of serialized_t will look something like:
[
{id:7, name_t:'test_1', e:{id:1, name_e:'ename_1'}},
{id:9, name_t:'test_2', e:{id:1, name_e:'ename_1'}}
]
My question is how could I combine it so that I can include Q in the query above to create an output for the T query which would also include all Q's where e_id=1 and hence it would look something like this:
[
{id:7, name_t:'test_1', e:{id:1, name_e:'ename_1'}, q:[{id:4, name_q:'qname_1'}, {id:5, name_q:'qname_2'}]},
{id:9, name_t:'test_2', e:{id:1, name_e:'ename_1'}, q:[{id:4, name_q:'qname_1'}, {id:5, name_q:'qname_2'}]}
]
I don't mind if this can be done in-query itself, or if it involves some appending to the SerializedT object, as long as I have a serialized result at the end of it which matches my needs that I can send as a JSON object as an API response.