ContentNotRenderedError:响应内容必须在迭代之前呈现

时间:2018-03-30 07:16:06

标签: python django

返回自定义响应时我有渲染错误。 我使用的是django 1.8和DRF 2.4.8

视图是

class UserList(generics.ListAPIView):
# queryset = User.objects.all()
serializer_class = UserSerializer

def get_queryset(self):
    is_staff = self.request.GET.get('is_staff', None)
    is_superuser = self.request.GET.get('is_superuser', None)
    is_admin = self.request.GET.get('is_admin', None)
    company = self.request.GET.get('company', None)
    userdetails = UserData(self.request.user).userdetails()
    print(userdetails.get('usertype'))
    query1 = query = Q()
    if is_staff:
        query1.add(Q(is_staff=is_staff), Q.AND)
    if is_superuser:
        query1.add(Q(is_superuser=is_superuser), Q.AND)
    if is_admin:
        query = Q(is_admin=is_admin)
    if company:
        query.add(Q(company__name=company), Q.AND)
    if userdetails.get('usertype') is 'superuser':
        if is_admin or company:
            profile_list = Profile.objects.filter(query).values_list('user_id', flat=True)
            user_list = User.objects.filter(id__in=profile_list)
            return user_list.filter(query1)
        else:
            return User.objects.filter(query1)
    elif userdetails.get('usertype') in ['staff', 'admin']:
        up = Profile.objects.get(user=self.request.user)
        profile_list = Profile.objects.filter(company=up.company)  # .values_list('user_id', flat=True)
        user_list = profile_list.filter(query).values_list('user_id', flat=True) if is_admin or company else \
            profile_list.values_list('user_id', flat=True)
        final_list = User.objects.filter(id__in=user_list)
        return final_list.filter(query1)
    else:
        return Response(data={"error": ["You don't have enough permission."]}, status=status.HTTP_400_BAD_REQUEST)

从最后一个部分返回响应时我收到错误? 解决办法是什么? Response()中有什么问题?

1 个答案:

答案 0 :(得分:0)

get_queryset应该返回QuerySet,而不是Response。 然而,通过提出validation exception

,您仍然可以表现得无效
def get_queryset(self):
    ...
    else:
        raise ValidationException({"error": ["You don't have enough permission."]})