Closed elcolie closed 6 years ago
After 5 months I am totally forgot.
I apologize for not seeing this earlier, but your problem is exactly the error -- you are exceeding the maximum recursion depth. You probably have a cycle in your data structure, so the recursion will proceed without end.
Hi. Thanks for getting back to the issue. It should produce the error. Since it is recursive by nature. And eager loading is not a recommend practice. I re-design to let my serializer becomes lazy-loader. Let it read through node by node not.
from rest_framework import serializers
from poinkbackend.apps.categories.models import Category
from poinkbackend.apps.menus.api.serializers import MenuSerializer
class ChildCategorySerializer(serializers.ModelSerializer):
"""Design to let it be a shallow detail"""
url = serializers.HyperlinkedIdentityField(view_name='api:category-detail')
image = serializers.ImageField(read_only=True)
small_thumbnail = serializers.ImageField(read_only=True)
medium_thumbnail = serializers.ImageField(read_only=True)
large_thumbnail = serializers.ImageField(read_only=True)
class Meta:
model = Category
fields = [
'url',
'id',
'name',
'image',
'small_thumbnail',
'medium_thumbnail',
'large_thumbnail',
]
class CategoryTreeSerializer(serializers.ModelSerializer):
...
children = ChildCategorySerializer(many=True)
parent = ChildCategorySerializer()
class Meta:
model = Category
fields = [
...
'parent',
'children',
]
models.py
Serializers.py
viewsets.py
I got the error
maximum recursion depth exceeded
Where am I wrong?