Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

325
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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')
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!