heywbj/django-rest-framework-recursive

Specify a recursivity depth

achedeuzot opened this issue · 6 comments

Hi !
Thanks for this great plugin for django rest framework. Is there any way to specify the depth of the recursion ?
I tried using the class Meta depth attribute but I don't think the field uses it. What would be the best approach to this ?
Best regards,

This is not currently supported. I would suggest pruning the dataset before you try to serialize it. If you are deserializing something, then you could deserialize it and then prune.

I'm using it on a ModelViewset/ModelSerializer so I don't know how I could prune it ?
There is no queryset or get_queryset method in the arguments of the field...

Can you set the queryset in the ModelViewSet?

No... My datastructure is really simple:

# models.py
class Node(models.Model):
    parent = models.ForeignKey('self', related_name='children')

# api.py
class NodeSerializer(serializers.HyperlinkedModelSerializer):

    parent = serializers.HyperlinkedRelatedField(
        view_name='api:parameter-detail',
        queryset=Node.objects.all(),
    )
    children = RecursiveField(many=True, allow_null=True)

    class Meta:
        model = Node
        fields = ('parent', 'children')
        depth = 2

class NodeViewSet(viewsets.ModelViewSet):
    queryset = Node.objects.all()

I cannot prune or filter the queryset. It will depend on which initial node is requested. I need to limit the depth of the RecursiveField only... Or maybe I'm missing something ?

You could look in to the django treebeard package for a tree with more features. Or django mptt. I believe there are probably others as well.

Or I suppose you could extend the recursive field to add the functionality your desire

I'll just extend the recursive field to add the functionality I need. Both mptt and treabeard have limitations or defaults that don't work with my project.
Thanks.