Using Python 3.x and the Django Rest Framework. I have a serializer with a Recursive Field (on self) which works as expected. However, I need a way to initially filter the nested children it returns by active = True.
I've tried different ways to filter children by active=True, but I'm unable to get this working on the nested children that are returned in the serializer.
Here is what I have.
class MenuListSerializer(serializers.ModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='menu_detail')
children = RecursiveField(many=True, required=False)
class RecursiveField(serializers.Serializer):
"""
Self-referential field for MPTT.
"""
def to_representation(self, value):
serializer = self.parent.parent.__class__(value, context=self.context)
return serializer.data
This is what I have tried but get the error ListSerializer' object has no attribute 'queryset' However, I'm not even sure this would work.
class MenuListSerializer(serializers.ModelSerializer):
def __init__(self, *args, request_user=None, **kwargs):
# try and filter active in chrildrend before query set is passed
super(MenuListSerializer, self).__init__(*args, **kwargs)
# print(self.fields['children'].parent)
self.fields['children'].queryset = self.fields['children'].queryset.filter(active=True)
url = serializers.HyperlinkedIdentityField(view_name='menu_detail')
children = RecursiveField(many=True, required=False)