heywbj / django-rest-framework-recursive

Recursive Serialization for Django REST framework
ISC License
367 stars 39 forks source link

Specify a recursivity depth #11

Closed achedeuzot closed 8 years ago

achedeuzot commented 8 years ago

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,

heywbj commented 8 years ago

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.

achedeuzot commented 8 years ago

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...

heywbj commented 8 years ago

Can you set the queryset in the ModelViewSet?

achedeuzot commented 8 years ago

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 ?

heywbj commented 8 years ago

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

achedeuzot commented 8 years ago

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.