我正在尝试创建一个帖子请求,以将作业应用程序添加到申请人列表中。这是由POST /task-worker/<task_id>
然而,我收到错误:
'Request'对象没有属性'task'
的 Models.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):
...
serializers.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'
答案 0 :(得分:3)
在序列化程序的create方法中,您不需要此行validated_data['task_id'] = self.context['request'].task
。任务将自动添加。
如果你有这个,你也不需要validated_data['worker'] = self.context['request'].worker
。
来自文档:
调用.create()或.update()时,任何其他关键字参数都将包含在validated_data参数中。