简体   繁体   English

Django REST Framework:尝试获取序列化器上字段的值时发生AttributeError

[英]Django REST Framework: AttributeError when attempting to get a value for field on serializer

I'm having trouble trying to correct the following error: AttributeError at /api/v1/tasks/queue/ Got AttributeError when attempting to get a value for field start_at on serializer TaskSerializer . 我有麻烦试图纠正以下错误:在AttributeError的/ API / V1 /任务/队列/ GOT AttributeError的企图获取字段值时, start_at上串行TaskSerializer The serializer field might be named incorrectly and not match any attribute or key on the bytes instance. 序列化程序字段的名称可能不正确,并且与bytes实例上的任何属性或键都不匹配。 Original exception text was: 'bytes' object has no attribute 'start_at'. 原始异常文本为:'bytes'对象没有属性'start_at'。

The error happens when i send a request to the API tasks/queue, i'm having a 500 Internal Server Error. 当我向API任务/队列发送请求时发生错误,我遇到500内部服务器错误。

I've tried several solutions presented on similar problems here on StackOverflow, including changing the field and serializer, but to no avail. 我已经尝试过在StackOverflow上针对类似问题提出的几种解决方案,包括更改字段和序列化器,但无济于事。

Posted below is relevant code snippets: 以下是相关的代码段:

serializers.py serializers.py


class TaskSerializer(serializers.ModelSerializer):
    start_at = serializers.DateField(format=settings.api_settings.DATE_FORMAT)

    class Meta:
        model = Task
        fields = '__all__'
        depth = 2

views.py views.py


class TaskQueue(generics.ListCreateAPIView):
    serializer_class = TaskSerializer

    def get_queryset(self):
        queryset = Task.objects.all()
        update_histogram()
        role = self.request.query_params.get('role', None)
        user = self.request.user
        if authorization(user,1) and role in ['camp', 'prod']:
            if Task.objects.filter(status='todo', service__role=role).exclude(service__type='incident').count() <= 1:
                return Task.objects.filter(status='todo', service__role=role).exclude(service__type='incident')
            else:
                queue = queryset.filter(status='todo', previous_task__isnull=True, next_task__isnull=False, service__role=role).exclude(service__type='incident')
                node = queue
                while node.first().next_task:
                    node = Task.objects.filter(previous_task=node.first())
                    queue = queue | node
                return queue
        else:
            return HttpResponse(status=404)

The expected output is a json with the object, but instead i'm getting an error 500. 预期的输出是带有该对象的json,但是相反,我遇到了错误500。

Any help would be very appreciated. 任何帮助将不胜感激。

The TaskQueue.get_queryset method is returning an HttpResponse . TaskQueue.get_queryset方法返回HttpResponse

View.get_queryset when overridden must return a QuerySet and no other object. View.get_queryset时的View.get_queryset必须返回QuerySet且不返回其他对象。

In order to resolve an unauthorized request to a 404, return an empty QuerySet or move this check higher up using a permission class. 为了解决对404的未授权请求,请返回一个空的QuerySet或使用权限类将此检查上移。

class TaskQueue(generics.ListCreateAPIView):
    serializer_class = TaskSerializer

    def get_queryset(self):
        # ...
        if authorization(user,1) and role in ['camp', 'prod']:
            #...
        else:
            return Task.objects.none()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 序列化程序 - Django REST 框架(AttributeError:在尝试获取序列化程序上的字段 `user` 的值时出现 AttributeError) - Serializers - Django REST framework (AttributeError: Got AttributeError when attempting to get a value for field `user` on serializer) Django REST 框架(AttributeError: Got AttributeError when trying to get a value for field "" on serializer "") - Django REST Framework (AttributeError : Got AttributeError when attempting to get a value for field " " on serializer " ") Django Rest Framework:尝试获取序列化程序“OrderCreateSerializer”上的字段“data_params”的值时出现 AttributeError - Django Rest Framework : Got AttributeError when attempting to get a value for field `data_params` on serializer `OrderCreateSerializer` Django序列化:尝试获取序列化器上字段的值时发生AttributeError - Django serialization: AttributeError when attempting to get a value for field on serializer Django Rest 框架属性错误:尝试获取字段值时出现属性错误 - Django Rest Framework AttributeError: Got AttributeError when attempting to get a value for field 尝试获取序列化程序上的字段值时出现 AttributeError - AttributeError when attempting to get a value for field on serializer Django DRF AttributeError:尝试获取序列化程序“UserMailListSerializer”上字段“ added_email”的值时出现 AttributeError - Django DRF AttributeError: Got AttributeError when attempting to get a value for field `added_email` on serializer `UserMailListSerializer` Django / DRF - 尝试在序列化程序“TestTakerSerializer”上获取字段“users_answers_set”的值时出现 AttributeError - Django / DRF - Got AttributeError when attempting to get a value for field `users_answers_set` on serializer `TestTakerSerializer` 尝试获取序列化程序“UserSerializer”上的字段“Name”的值时出现 AttributeError - Got AttributeError when attempting to get a value for field `Name` on serializer `UserSerializer` 尝试在序列化程序“ResidenceSerializer”上获取字段“hotel”的值时出现 AttributeError - Got AttributeError when attempting to get a value for field `hotel` on serializer `ResidenceSerializer`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM