22

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)
4
  • 37
    Why using many=True. Parent is just a single field, no need to use explicit serializer field. Just get rid of these many=True.. Commented Nov 2, 2014 at 19:59
  • I misunderstood the docs in the same way. Commented May 2, 2015 at 21:00
  • Yeah, just remove those many=True, also works for me Commented Feb 28, 2017 at 18:59
  • @mariodev could you please make your comment as the answer. I feel like I am taking credits of your answer. Commented Aug 9, 2017 at 17:31

4 Answers 4

50

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.

Sign up to request clarification or add additional context in comments.

Comments

0
You can do something like this using python collections as an intermediate

#serializers.py
class TimelineSerializer(serializers.Serializer):
    childs= childSerializer(many=True)
    parents = parentSerializer(many=True)

#apiviews.py
from collections import namedtuple
Timeline = namedtuple('Timeline', ('childs', 'parents'))

def list(self, request):
        timeline = Timeline(
            childs=Child.objects.all(),
            parents=Parent.objects.all(),
        )
        serializer = TimelineSerializer(timeline)
        return Response(serializer.data)

Comments

0

If your using a ModalSerializer then you have two foreign keys in a modal, then use "to_representation" function like this

def to_representation(self, instance):
        rep = super().to_representation(instance)
        rep['pcatalog_name'] = CatalogSerializer(instance.pcatalog_name).data
        rep['pcategory_name'] = CategorySerializer(instance.pcategory_name).data
        return rep

Comments

0

Replace ForeignKey by ManyToManyField to clarify the serializer field with many = True

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.