class TestSerializer(Serializer):
field1 = fields.CharField()
field2 = fields.CharField()
field3 = fields.BooleanField()
type = PrimaryKeyRelatedField(queryset=Type.objects.all(), many=True)
filed5 = PrimaryKeyRelatedField(queryset=Field5Obj.objects.all(), many=True)
field6 = Field6Serializer()
class TypeSerializer(ModelSerializer):
class Meta:
model = Type
fields = '__all__'
class TestSerializer(serializers.Serializer):
field1 = fields.CharField()
field2 = fields.CharField()
field3 = fields.BooleanField()
types = serializers.SerializerMethodField()
field4 = Field4Serializer()
def get_types(self, obj):
return [TypeSerializer(
instance=instance
).data for instance in Type.objects.all()]