I serialized one of my models in which there is a foreign key. I get a 'Parent' object is not iterable
models.py
class Parent(models.Model):
serializer.py
class ChildSerializers(serializers.ModelSerializer): parent = serializers.RelatedField(many=True) class Meta: model = ReportField fields = ( 'id', 'parent' )
api.py
class ChildList(APIView): def get(self, request, format=None): child = Child.objects.all() serialized_child = ChildSerializers(child, many=True) return Response(serialized_child.data)
I assume I need to pass the parent list to a list of children, but not sure if this is the best way to do this
attempt api.py
class ChildList(APIView): def get(self, request, format=None): child = Child.objects.all() parent = Parent.objects.all() serialized_child = ChildSerializers(child, many=True) serialized_parent = ChildSerializers(parent, many=True) return Response(serialized_child.data, serialized_parent.data)
source share