Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

327
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda