我在"中收到这些错误AttributeError:' NoneType'对象没有属性' get_all_permissions'

时间:2016-07-15 09:45:13

标签: python django-rest-framework

class TenantViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows Tenant users to be viewed.
    """
    model = TenantUser
    serializer_class = TenantUserSerializer

    def list(self, request, domain):
        tenants = TenantUser.objects.all()
        serializer = TenantUserSerializer(tenants)
        return Response(serializer.data)

    def retrieve(self, request, domain, pk=None):
        current_tenant_user = get_current_tenant_user()
        permissions = current_tenant_user.user.get_all_permissions()
        if pk == 'me' or int(pk) == current_tenant_user.id:
            tenant_user = current_tenant_user
        else:
            if not 'core.admin' in permissions:
                return Response({"detail": "You don't have permission to perform this action."},
                                status=status.HTTP_403_FORBIDDEN)
            try:
                tenant_user = TenantUser.objects.get(id=pk)
            except(TenantUser.DoesNotExist, Exception, ValueError):
                return Response(status=status.HTTP_404_NOT_FOUND)
            permissions = tenant_user.user.get_all_permissions()
        serializer = TenantUserSerializer(tenant_user)
        serializer.data['is_apps_admin'] = 'core.apps' in permissions
        serializer.data['is_admin'] = 'core.admin' in permissions
        serializer.data['is_user'] = 'core.user' in permissions
        serializer.data['is_subscribed'] = 'core.active' in permissions
        serializer.data['is_sales_admin'] = 'core.sales' in permissions
        serializer.data['is_marketing_admin'] = 'core.marketing' in permissions
        return Response(serializer.data)

1 个答案:

答案 0 :(得分:0)

我的猜测 get_current_tenant_user 功能不会返回用户。确保用户使用 return yield 语句从该功能返回。