Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework object is not iterable?

Tags:

python

django

I have serialised one of my models that has a foreign key in. I get 'Parent' object is not iterable

models.py

class Parent(models.Model):
    # Parent data

class Child(models.Model):
    parent = ForeignKey(Parent)

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)

Im guessing i have to pass the parent list to the child list but not sure of the best way to do it

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)
like image 734
Chris Meek Avatar asked Nov 02 '14 18:11

Chris Meek


1 Answers

Why using many=True. Parent is just a single field, no need to use explicit serializer field. Just get rid of these many=True

-answered by mariodev in comment.

like image 111
Praveen Singh Yadav Avatar answered Nov 19 '22 01:11

Praveen Singh Yadav