Simon Johansson
Simon Johansson

Reputation: 836

Filtering Many-to-many field in Rest Framework

Im working with the Django REST Framwork and got some issues with the Browsable api. I cannot filter a ManyToMany field so that it only shows the objects which the user is owner of. I do manage to filter the the user and filter out m own objects.

In the serializer I have

class BidSerializer(serializers.HyperlinkedModelSerializer):
        id = serializers.HyperlinkedRelatedField(view_name='bid-detail', read_only=True)

    def __init__(self, *args, **kwargs):
        super(BidSerializer, self).__init__(*args, **kwargs)
        request_user = self.context['request'].user
        self.fields['my_items'].queryset  = Item.objects.filter(owner=request_user)
        print(self.fields['my_items'].queryset)
        self.fields['others_items'].queryset = Item.objects.exclude(owner=request_user)
        self.fields['user'].queryset = User.objects.filter(username=request_user)

    class Meta:
        model = Bid
        fields = ('id','comment','user','others_items','my_items','timestamp')

The strange thing is that the fields 'user' and 'others_items' are filtered as supposed. While 'my_items' is not filtered but showing all items. However the line containing the print statement shows a correct output. The difference between the fields is that my_items is a ManyToMany field and others_items is a foreign key.

Should it be possible to filter as I like? If not why and how could I filter my choices in a better way?

Upvotes: 0

Views: 534

Answers (1)

Brent O'Connor
Brent O'Connor

Reputation: 5881

I ran into this same issue and after inspecting one of my many-to-many fields in my debugger I discovered the child_relation property on the many-to-many field which had a queryset property. Adding child_relation property in front of the queryset worked for me.

I'm also using version 3.8.2 of the Django Rest Framework.

Example:

class BidSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.HyperlinkedRelatedField(view_name='bid-detail', read_only=True)

    def __init__(self, *args, **kwargs):
        super(BidSerializer, self).__init__(*args, **kwargs)
        request_user = self.context['request'].user
        self.fields['my_items'].child_relation.queryset = Item.objects.filter(owner=request_user)
        self.fields['others_items'].child_relation.queryset = Item.objects.exclude(owner=request_user)
        self.fields['user'].queryset = User.objects.filter(username=request_user)

    class Meta:
        model = Bid
        fields = ('id', 'comment', 'user', 'others_items', 'my_items', 'timestamp')

Upvotes: 1

Related Questions