Python django 休息框架过滤器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21374253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
django rest framework filter
提问by user3236034
I'm working with API made from Django rest framework,
I am trying to make a filter to a JSON
This is my serializers.pyfile
我正在使用由 Django rest 框架制作的 API,我正在尝试对 JSON 进行过滤 这是我的 serializers.py文件
from rest_framework import serializers
from .models import Establecimiento,Categoria,Ciudad,Zona
import django_filters
class EstablecimientoSerializer(serializers.ModelSerializer):
class Meta:
model = Establecimiento
depth = 1
fields = ('nombre',
'ciudad',
'categoria',
'direccion',
'telefono',
'precioMinimo',
'precioMaximo',)
and this my views.pyfile
这是我的views.py文件
from rest_framework import viewsets
from .serializers import EstablecimientoSerializer, CategoriaSerializer
from models import *
from rest_framework import filters
from rest_framework import generics
class EstablecimientoViewSet(viewsets.ModelViewSet):
queryset = Establecimiento.objects.all()
serializer_class = EstablecimientoSerializer
filter_fields = ('categoria',)
Then in the EstablecimientoViewSetclass, I put a filter_fields = ('categoria',)to filter the url's API with the category field
然后在EstablecimientoViewSet类中,我放了一个filter_fields = ('categoria',)用类别字段过滤url的API
If I add the filter to the query parameters, the result set does not change, it shows all data unfiltered.
如果我将过滤器添加到查询参数中,结果集不会改变,它会显示所有未过滤的数据。
...establecimiento?establecimiento=bar
How can I make this filter about this model?
我怎样才能制作关于这个模型的过滤器?
采纳答案by mariodev
You need to define filter backend and all related fields you're planning to filter on:
您需要定义过滤器后端和您计划过滤的所有相关字段:
class EstablecimientoViewSet(viewsets.ModelViewSet):
filter_backends = (filters.DjangoFilterBackend,)
filter_fields = ('categoria', 'categoria__titulo',)
example:
例子:
URL?categoria__titulo=Categoria 1
回答by DmitrySemenov
it's also possible to supply your own Filterclass, which may give you more options and flexibility
也可以提供你自己的Filter类,这可能会给你更多的选择和灵活性
import sys, django_filters, json, io
class TaskFilter(django_filters.FilterSet):
tag = django_filters.CharFilter(name='tags__name', lookup_type='iexact')
university = django_filters.NumberFilter(name='poster__university', lookup_type='exact')
class Meta:
model = Task
fields = {
'poster': ['exact'],
'tasker': ['exact'],
'status': ['exact'],
'created': ['lt', 'gt']
}
In this example I got filters
在这个例子中,我得到了过滤器
- poster = 1
- tasker = 115
- status = O
created__lt=2015-09-22 17:39:01.184681 (so I can filter datetime by values LESS THEN)
created__gt=2015-09-22 17:39:01.184681 (or GREATER THAN provided value)
- 海报 = 1
- 任务者 = 115
- 状态 = O
created__lt=2015-09-22 17:39:01.184681(所以我可以按值过滤日期时间 LESS THEN)
created__gt=2015-09-22 17:39:01.184681(或大于提供的值)
Also I can hide foreign fields with custom filter fields, in this case it's tag& university. Plus I can provide comparison operator (lookup_type)
我也可以使用自定义过滤器字段隐藏外部字段,在这种情况下它是tag& university。另外我可以提供比较运算符(lookup_type)
Sample request:
样品请求:
GET /api/v1/tasks/?offset=0&status=O&limit=100&university=1&ordering=-created&created__lt=2015-09-22 17:39:01.184681&tag=sport HTTP/1.1
Host: domain.com
Content-Type: application/json
Authorization: token 61cbd3c7c2656d4e24edb31f5923a86910c67b7c
User-Timezone: US/Pacific
Cache-Control: no-cache
回答by Thiago Soares
For me, it works when I put the comma at the end of my filter_fields.
对我来说,当我将逗号放在 filter_fields 的末尾时,它会起作用。
eg.
例如。
filter_fields = ('distribuidor',)

