扩展搜索并返回所有对象Django REST Framework

时间:2017-05-15 13:32:46

标签: django django-rest-framework django-filter

目前在我的ModelViewSet我今天返回所有联系人,此外我还要检查搜索/过滤请求中的query_params是否可用而非空,In那个案子我今天没有添加"#34;到queryset,以便对整个queryset执行搜索。

问题是我需要扩展搜索以便用户可以看到其他用户的联系人,但只有当他搜索它们时,如果你不搜索默认视图不应该改变,那么如何扩展我当前的过滤器并返回搜索中的所有对象。

我目前的观点:

from rest_framework import viewsets, permissions, filters

from cms.restapi.pagination import StandardResultsOffsetPagination
from cms_sales.models import LeadContact
from cms_sales.restapi.permissions.lead_contact_permissions import LeadContactPermissions
from cms_sales.restapi.serializers.lead_contact_serializer import LeadContactSerializer


class LeadContactViewSet(viewsets.ModelViewSet):
    def get_queryset(self):
        queryset = LeadContact.objects.none()
        user = self.request.user
        if user.has_perm('cms_sales.can_view_full_lead_contact_list'):
            queryset = LeadContact.objects.all()
        elif user.has_perm('cms_sales.can_view_lead_contact'):
            queryset = LeadContact.objects.filter(account_handler=user)

        filter_date = self.request.query_params.get('filter_date', None)
        search_params = self.request.query_params.get('search', None)

        if filter_date is not None and (search_params is None or len(search_params) == 0):
            queryset = queryset.filter(next_action_date=filter_date)

        return queryset

    serializer_class = LeadContactSerializer

    filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)

    filter_fields = ('account_handler',)

    ordering_fields = (
        'first_name', 'last_name', 'account_handler__first_name', 'account_handler__last_name',
        'sub_organization_name', 'organization_name', 'next_action_date', 'serial_number',
        'status_text')

    search_fields = (
        'first_name', 'last_name', 'account_handler__first_name', 'account_handler__last_name',
        'sub_organization_name', 'organization_name', 'next_action_date', 'serial_number',
        'status_text')

    pagination_class = StandardResultsOffsetPagination

    permission_classes = [permissions.IsAuthenticated, LeadContactPermissions]

Current Serializer:

from django.conf import settings
from django.contrib.auth.models import User
from rest_framework import serializers

from cms_sales.models import LeadContact


class AccountHandlerSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name')


class LeadContactSerializer(serializers.ModelSerializer):
    account_handler = AccountHandlerSerializer()
    next_action_date = serializers.DateTimeField(format=settings.CUSTOM_DATE_FORMAT_NO_TIME)
    absolute_url = serializers.URLField(source='get_absolute_url')

    class Meta:
        model = LeadContact
        fields = (
            'pk', 'organization_name', 'sub_organization_name', 'serial_number', 'account_handler', 'status_text',
            'first_name', 'last_name', 'next_action_date', 'absolute_url', 'status_display_class'
        )
        depth = 1

当前型号:

class LeadContact(models.Model):
    organization_name = models.CharField(max_length=200, blank=False, null=True)
    sub_organization_name = models.CharField(max_length=200, blank=False, null=True)
    serial_number = models.CharField(max_length=30, db_index=True, blank=True, null=True)
    account_handler = models.ForeignKey(User, blank=True, null=True, related_name='handling_leads', on_delete=models.SET_NULL)
    next_action_date = models.DateField(null=True, verbose_name="Next action on lead")
    status_text = models.CharField(max_length=20, default='', blank=True)
    first_name = models.CharField(_('first name'), max_length=30)
    last_name = models.CharField(_('last name'), max_length=30)

1 个答案:

答案 0 :(得分:1)

移动代码以检查检查搜索参数的代码下方的权限,以便在检查search_params后应用用户过滤器

def get_queryset(self):

    queryset = LeadContact.objects.all()

    if (not user.has_perm('cms_sales.can_view_full_lead_contact_list') and
        not user.has_parm('cms_sales.can_view_lead_contact')):
        return queryset.none()

    user = self.request.user

    filter_date = self.request.query_params.get('filter_date', None)
    search_params = self.request.query_params.get('search', None)

    if filter_date is not None and (search_params is None or len(search_params) == 0):
        queryset = queryset.filter(next_action_date=filter_date)


    if user.has_perm('cms_sales.can_view_lead_contact') and not search_params:
        queryset = queryset.filter(account_handler=user)


    return queryset