返回用户ID django

时间:2017-04-05 08:37:58

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

我有一个模型字段

account_handler = models.ForeignKey(User, blank=True, null=True, related_name='handling_leads', on_delete=models.SET_NULL)

目前我正在做这样的事情

def get_queryset(self):
    user_id = User.objects.all()
    queryset = User.objects.filter(handling_leads__account_handler=user_id).distinct()
    return queryset

我已经获得了一些测试结果。现在问题是,如何正确使用此字段并返回user_id?我尝试过这样的事情

User.objects.filter(handling_leads__account_handler=self.request.user.id).distinct()

没有User.objects.all(),但这只是返回一个空的查询集。我试图对此进行过滤,以便我可以使用user__first_nameuser__last_name填充表格,我需要user_id,以便我可以将它们重定向到各自的页面。

数据表的控制器:

app = angular.module 'statistics'

app.controller 'SalesCommissionsListCtrl', ['$scope', '$compile',
  ($scope, $compile) ->
    $scope.init = ()->
      fetchCommissionsList()

    fetchCommissionsList = ()->
      $('#commissionsSalesList').DataTable({
        createdRow: (row, data, index) ->
          $compile(row)($scope)
        sDom: 'lftip',
        processing: true,
        serverSide:true,
        searchDelay: 1000,
        orderMulti: false,
        pageLength: 10,
        ajax: {
          url: '/api/statistics/commissions/list/',
          data: (data)->
            data.limit = data.length;
            data.offset = data.start;
            data.search = data.search['value'];
            if (data.order[0]['dir'] == 'asc')
              data.ordering = data.columns[data.order[0]['column']].name;
            else
              data.ordering = '-' + data.columns[data.order[0]['column']].name;
            return 0
          dataFilter: (data) ->
            json = jQuery.parseJSON(data);
            json.recordsTotal = json.count;
            json.recordsFiltered = json.count;
            json.data = json.results;
            return JSON.stringify(json);
        }
        columns: [
          {
            data: "username",
            name: "username__first_name, username__last_name",
            render: (data, type, row, meta)->
              return '<a href="/statistics/commissions/sales/' + row.user_id + '">' + data + '</a>';
          }
        ]
      })
]

我的ModelViewSet

from django.contrib.auth.models import User
from rest_framework import viewsets, filters, permissions

from app.restapi.pagination import StandardResultsOffsetPagination
from statistics.restapi.serializers import SalesCommissionsSerializer
from statistics.restapi.permissions import SalesCommissionsListPermissions


class SalesCommissionsViewSet(viewsets.ModelViewSet):
    def get_queryset(self):
        user_id = User.objects.all()
        queryset = User.objects.filter(handling_leads__account_handler=user_id).distinct()
        return queryset

    serializer_class = SalesCommissionsSerializer

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

    filter_fields = (
        'id', 'username', 'first_name', 'last_name'
    )

    ordering_fields = (
        'id', 'username', 'first_name', 'last_name',
    )

    search_fields = (
        'id', 'username', 'first_name', 'last_name',
    )

    pagination_class = StandardResultsOffsetPagination

    permission_classes = [permissions.IsAuthenticated, SalesCommissionsListPermissions]

串行器:

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


class SalesCommissionsSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'first_name', 'last_name')

1 个答案:

答案 0 :(得分:1)

如果您要过滤处理潜在客户的用户,您可以执行以下操作:

handling_leads = User.objects.filter(handling_leads__isnull=False)

这是指向文档的链接:https://docs.djangoproject.com/en/1.10/ref/models/querysets/#std:fieldlookup-isnull