gpt4 book ai didi

python - Django 休息 : 'Request' object has no attribute 'field_name'

转载 作者:行者123 更新时间:2023-11-30 22:21:45 24 4
gpt4 key购买 nike

我正在尝试创建一个发布请求以将工作申请添加到申请人列表中。这是由 POST /task-worker/<task_id> 完成的

但是,我收到错误:

'Request' object has no attribute 'task'

模型.py

class Task_worker(models.Model):
worker = models.ForeignKey(Worker)
task = models.ForeignKey(Task)

class Meta:
unique_together = ('worker', 'task')

class Task(models.Model):
...

class Worker(models.Model):
...

序列化器.py

class TaskWorkerSerializer(serializers.ModelSerializer):
task = serializers.ReadOnlyField(source='task.id')
worker = serializers.ReadOnlyField(source='worker.id')

class Meta:
model = Task_worker
fields = ('id', 'task', 'worker', 'created', )

def create(self, validated_data):
validated_data['task_id'] = self.context['request'].task
return super(TaskWorkerSerializer, self).create(validated_data)

views.py

class TaskWorker(generics.ListCreateAPIView):
serializer_class = TaskWorkerSerializer

def get_queryset(self):
task_id = self.kwargs.get('task_id', '')
queryset = Task_worker.objects.filter(task__user=self.request.user, task_id=task_id).distinct()
return queryset

def perform_create(self, serializer):
worker = Worker.objects.get(user=self.request.user)
task = Task.objects.get(pk=self.kwargs.get('task_id', ''))
serializer.save(task=task, worker=worker)
#ALSO TRIED
#serializer.save(task_id=self.kwargs.get('task_id', ''), worker=worker)

即使我没有 task明确在perform_create()中,我仍然收到相同的错误,说请求没有任务属性。

urls.py

url(r'^task-worker/(?P<task_id>[0-9]+)/$', views.TaskWorker.as_view()),
<小时/>

编辑:完整回溯。

Traceback:  

File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/request.py" in __getattr__
410. return getattr(self._request, attr)


During handling of the above exception ('WSGIRequest' object has no attribute 'task'), another exception occurred:



File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)

File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)

File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)

File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)

File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
494. response = self.handle_exception(exc)

File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/views.py" in handle_exception
454. self.raise_uncaught_exception(exc)

File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
491. response = handler(request, *args, **kwargs)

File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/generics.py" in post
244. return self.create(request, *args, **kwargs)

File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/mixins.py" in create
21. self.perform_create(serializer)

File "/Users/gr/Desktop/PycharmProjects/godo/api/views.py" in perform_create
42. serializer.save(task_id=self.kwargs.get('task_id', ''), worker=worker)

File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/serializers.py" in save
214. self.instance = self.create(validated_data)

File "/Users/gr/Desktop/PycharmProjects/godo/api/serializers.py" in create
25. validated_data['task_id'] = self.context['request'].task

File "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/request.py" in __getattr__
412. return self.__getattribute__(attr)

Exception Type: AttributeError at /api/task-worker/17/
Exception Value: 'Request' object has no attribute 'task'

最佳答案

在序列化程序的创建方法中,您不需要此行 validated_data['task_id'] = self.context['request'].task 。任务将自动添加。

如果你有这个,你也不需要 validated_data['worker'] = self.context['request'].worker

来自文档:

Any additional keyword arguments will be included in the validated_data argument when .create() or .update() are called.

关于python - Django 休息 : 'Request' object has no attribute 'field_name' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48542211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com