如何使用mongoengine进行django rest过滤

时间:2015-07-27 07:06:15

标签: django mongodb python-2.7 django-rest-framework mongoengine

您好我正在使用mongo引擎启动django 1.8.3以创建rest api。

我正在使用rest_framework_mongoengine这样做 我想使用DjangoFilterBackend的一个功能。

我的代码是:

models.py:

from mongoengine import *    
from django.conf import settings    
connect(settings.DBNAME)

class Client(Document):
    name = StringField(max_length=50)
    city = StringField(max_length=50)
    country = StringField(max_length=200, verbose_name="Country")
    address = StringField(default='')

Serializer.py

from client.models import Client    
from rest_framework_mongoengine.serializers import DocumentSerializer    


class ClientSerializer(DocumentSerializer):
    class Meta:
        model = Client
        depth = 1

views.py

from rest_framework_mongoengine.generics import *    
from rest_framework import filters    


class ClientList(ListCreateAPIView):
    serializer_class = ClientSerializer
    queryset = Client.objects.all()
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = ('name',)

我开始收到错误     QuerySet对象没有属性模型

不知道哪里出错了。如果我删除filter_field它可以工作,但我不能使用过滤器功能。

任何帮助都会很有用

2 个答案:

答案 0 :(得分:4)

您还可以通过覆盖get_queryset()方法并创建通用过滤功能来执行过滤。

在这里,我们将视图中的过滤器字段元组指定为我们要执行过滤的my_filter_fields。然后在我们的get_queryset()中,我们调用函数get_kwargs_for_filtering()

get_kwargs_for_filtering()函数迭代my_filter_fields中定义的字段,并检查是否在query_params中传递了这些字段。如果找到该字段,则在字典filtering_kwargs中设置具有字段名称和值作为检索值的键。迭代结束后,此filtering_kwargs字典将返回到get_queryset()方法。

filtering_kwargs字典用于过滤查询集。

from rest_framework_mongoengine.generics import *    
from rest_framework import filters    


class ClientList(ListCreateAPIView):
    serializer_class = ClientSerializer
    my_filter_fields = ('name', 'country') # specify the fields on which you want to filter

    def get_kwargs_for_filtering(self):
        filtering_kwargs = {} 
        for field in  self.my_filter_fields: # iterate over the filter fields
            field_value = self.request.query_params.get(field) # get the value of a field from request query parameter
            if field_value: 
                filtering_kwargs[field] = field_value
        return filtering_kwargs 

    def get_queryset(self):
        queryset = Client.objects.all() 
        filtering_kwargs = self.get_kwargs_for_filtering() # get the fields with values for filtering 
        if filtering_kwargs
            queryset = Client.objects.filter(**filtering_kwargs) # filter the queryset based on 'filtering_kwargs'
        return queryset

答案 1 :(得分:1)

这是完成此工作的好方法。 首先,您应该安装django_mongoengine_filter rest_framework_mongoengine

pip install django_mongoengine_filter 
pip install rest_framework_mongoengine

第二,您可以这样编写所有者过滤器:

import django_mongoengine_filter as filters

from app.models import User

class UserFilter(filters.FilterSet):
    class Meta:
        model = User
        fields = ['name']

最后,您可以在视图类中使用像这样的UserFilter:

from app.models import User
from app.serializer import UserS  # use rest_framework_mongoengine to write serializer
from app.filters import UserFilter


class UserVS(ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserS
    # override filter_queryset function
    def filter_queryset(self, queryset):
        filter = UserFilter(self.request.query_params, queryset=queryset)
        return filter.qs