AttributeError:'module'对象没有属性'DjangoFilterBackend'

时间:2018-04-12 11:52:57

标签: django django-filter

我正在使用django-filter但我得到以下错误:'module'对象没有属性'DjangoFilterBackend' 我发送的代码如下:

Model.py

    from django.db import models

# Create your models here.
class searchModel(models.Model):
    email = models.CharField(max_length=25)
    password = models.CharField(max_length=10)
    image    = models.ImageField(upload_to='Image/')

    def __str__(self):
        return "%s" %  self.email

serializers.py

from rest_framework import serializers
from .models import searchModel

class searchserializer(serializers.ModelSerializer):
    image = serializers.ImageField(max_length=None, use_url=True)

    class Meta:
        model=searchModel
        fields=('email','password','image')

Views.py

from rest_framework import viewsets
from .models import searchModel
from .serializers import searchserializer
#from django_filters.rest_framework import filters
from rest_framework import filters

class searchView(viewsets.ModelViewSet):
    queryset=searchModel.objects.all()
    serializer_class = searchserializer
    filter_backends = (filters.DjangoFilterBackend,filters.OrderingFilter,)
    filter_fields = ('email')
    ordering = ('email')

4 个答案:

答案 0 :(得分:5)

使用rest framework docs中的建议导入,您可以执行以下操作:

import django_filters.rest_framework

class searchView(viewsets.ModelViewSet):
    ...
    filter_backends = (django_filters.rest_framework.DjangoFilterBackend,filters.OrderingFilter,)

或者,您可以

from django_filters.rest_framework import DjangoFilterBackend

class searchView(viewsets.ModelViewSet):
    ...
    filter_backends = (DjangoFilterBackend, filters.OrderingFilter,)

答案 1 :(得分:0)

它将为您服务 检查以下代码:

from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters

class TaskViewSet(viewsets.ModelViewSet):
  queryset = Task.objects.all().order_by('-date_created')
  serializer_class = TaskSerializers
  filter_backends = (DjangoFilterBackend, filters.OrderingFilter)

答案 2 :(得分:0)

使用此导入即可:

import django_filters.rest_framework
filter_backends = (django_filters.rest_framework.DjangoFilterBackend,)

答案 3 :(得分:0)

相同-原因是FilterSet是django_filters的一部分,而不是rest_framekwork的一部分,所以:

...

删除“来自rest_framework导入过滤器”行

... 从django_filters导入FilterSet,AllValuesFilter,DateTimeFilter,NumberFilter