简体   繁体   English

在Rest Framework中过滤多对多字段

[英]Filtering Many-to-many field in Rest Framework

Im working with the Django REST Framwork and got some issues with the Browsable api. 我正在使用Django REST Framwork,但Browsable api出现了一些问题。 I cannot filter a ManyToMany field so that it only shows the objects which the user is owner of. 我无法过滤ManyToMany字段,因此它仅显示用户所属的对象。 I do manage to filter the the user and filter out m own objects. 我确实设法过滤用户并过滤出m个对象。

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. 奇怪的是,字段“ user”和“ others_items”按预期方式进行了过滤。 While 'my_items' is not filtered but showing all items. 虽然“ my_items”未过滤,但显示了所有项目。 However the line containing the print statement shows a correct output. 但是,包含print语句的行显示正确的输出。 The difference between the fields is that my_items is a ManyToMany field and others_items is a foreign key. 字段之间的区别在于my_items是ManyToMany字段,others_items是外键。

Should it be possible to filter as I like? 是否应该可以按我的意愿进行过滤? If not why and how could I filter my choices in a better way? 如果不是,为什么以及如何更好地过滤我的选择?

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. 我遇到了同样的问题,在调试器中检查了我的多对多字段之一后,我发现了具有child_relation属性的多对多字段上的queryset属性。 Adding child_relation property in front of the queryset worked for me. 在queryset前面添加child_relation属性对我来说很有效。

I'm also using version 3.8.2 of the Django Rest Framework. 我还使用了Django Rest Framework的3.8.2版本。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM