如何通过slug代替id过滤对象

时间:2019-02-05 21:05:52

标签: python django django-rest-framework

我有一个事件模型,序列化程序,视图集和一个标记模型,序列化程序,视图集。我想要按标签名过滤事件。但是只能通过tag_id过滤它

在标签序列化器中,我在TagSerializer中设置了lookup_field。当我'filter_field =(tags__name)'时该选项不合适,因为最后一个GET参数更改了上一个

class Event(models.Model):


tags = models.ManyToManyField('Tag')

...


class Meta:
    ordering = ['name']

def __str__(self):
    return self.name


class Tag(models.Model):
  name = models.CharField(unique=True, max_length=50)

  def __str__(self):
      return self.name

  def __unicode__(self):
      return '%s'% (self.name)



class EventSerializer(serializers.ModelSerializer):
  from accounts.api.serializers import ShortUserSerializer
  tags = serializers.SlugRelatedField(
      many=True,
      queryset=Tag.objects.all(),
      slug_field='name'
  )

  members_count = serializers.SerializerMethodField()

  author = ShortUserSerializer()

  max_members = serializers.IntegerField(required=False, default=-1)

  class Meta:
      model = Event
      fields = ['id', 'name', 'description', 'time_begins', 'author', 'members_count', 'max_members', 'tags',
              'avatar', 'date_expire', 'city', 'country', 'geo']
 ...

class TagSerializer(serializers.ModelSerializer):
  class Meta:
      model = Tag
      fields = '__all__'
      lookup_field = 'name'
      extra_kwargs = {
          'url': {'lookup_field': 'name'}
      }
...

class EventViewSet(viewsets.ModelViewSet):
  queryset = Event.objects.all()
  serializer_class = EventSerializer
  http_method_names = ['get', 'patch']
  filter_backends = (filters.SearchFilter, DjangoFilterBackend,)
  search_fields = ('name', 'author__username', )
  filter_fields = ('tags')

我收到以下结果:

 `localhost:8000/events/?tags=1&tags=2`

它实际上有效

但是我需要:

 `localhost:8000/events/?tags=#fun&tags=#movie`

2 个答案:

答案 0 :(得分:1)

filter_fields生成的默认过滤器集无法解决这种情况。对于外国关系,它使用这个想法。

因此,您必须像这样定义自定义过滤器集

class EventFilter(filters.FilterSet):
    tags = filters.CharFilter(field_name="tags__name", method='filter_tags')

    class Meta:
        model = Event
        fields = ['tags']

    def filter_tags(self, queryset, name, tags):
        return queryset.filter(tags__name__contains=tags.split(','))

然后,您需要删除filter_fields并将其添加为filter_class中的EventViewSet

class EventViewSet(viewsets.ModelViewSet):
  queryset = Event.objects.all()
  serializer_class = EventSerializer
  http_method_names = ['get', 'patch']
  filter_backends = (filters.SearchFilter, DjangoFilterBackend,)
  search_fields = ('name', 'author__username', )
  filter_class = EventFilter

答案 1 :(得分:0)

如果您尝试像这样filterset_fields = ['tags__name', ]

使用该怎么办

,然后通过?tags__name=name

访问