我正在通过Django Rest Framework教程工作,我被困在第4部分。
在为视图添加权限后,它会说创建几个片段。我通过可浏览的API登录并尝试发布这样的代码段:
{
"title": "snippet",
"code": "print 123"
}
当我这样做时:
/ snippets /
中的IntegrityErrorNOT NULL约束失败:snippets_snippet.owner_id
任何人都知道这里发生了什么?
Traceback:
File "/home/grez/restenv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/grez/restenv/lib/python3.4/site-packages/django/views/decorators/csrf.py" in wrapped_view
57. return view_func(*args, **kwargs)
File "/home/grez/restenv/lib/python3.4/site-packages/django/views/generic/base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "/home/grez/restenv/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
403. response = self.handle_exception(exc)
File "/home/grez/restenv/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
400. response = handler(request, *args, **kwargs)
File "/home/grez/web/resttutorial/snippets/views.py" in post
23. serializer.save()
File "/home/grez/restenv/lib/python3.4/site-packages/rest_framework/serializers.py" in save
599. self.save_object(self.object, **kwargs)
File "/home/grez/restenv/lib/python3.4/site-packages/rest_framework/serializers.py" in save_object
1041. obj.save(**kwargs)
File "/home/grez/web/resttutorial/snippets/models.py" in save
36. super(Snippet, self).save(*args, **kwargs)
File "/home/grez/restenv/lib/python3.4/site-packages/django/db/models/base.py" in save
591. force_update=force_update, update_fields=update_fields)
File "/home/grez/restenv/lib/python3.4/site-packages/django/db/models/base.py" in save_base
619. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/grez/restenv/lib/python3.4/site-packages/django/db/models/base.py" in _save_table
700. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/grez/restenv/lib/python3.4/site-packages/django/db/models/base.py" in _do_insert
733. using=using, raw=raw)
File "/home/grez/restenv/lib/python3.4/site-packages/django/db/models/manager.py" in manager_method
92. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/grez/restenv/lib/python3.4/site-packages/django/db/models/query.py" in _insert
921. return query.get_compiler(using=using).execute_sql(return_id)
File "/home/grez/restenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in execute_sql
920. cursor.execute(sql, params)
File "/home/grez/restenv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
81. return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/grez/restenv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
File "/home/grez/restenv/lib/python3.4/site-packages/django/db/utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/grez/restenv/lib/python3.4/site-packages/django/utils/six.py" in reraise
549. raise value.with_traceback(tb)
File "/home/grez/restenv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
File "/home/grez/restenv/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py" in execute
485. return Database.Cursor.execute(self, query, params)
Exception Type: IntegrityError at /snippets/
Exception Value: NOT NULL constraint failed: snippets_snippet.owner_id
答案 0 :(得分:3)
我今晚正在浏览本教程并遇到同样的错误。看来教程是一个版本左右的实际框架可能呢?无论如何,经过一些研究后,我能够获得片段保存。
教程说这样做:
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
我切换到了这个:
def pre_save(self, snip):
snip.owner = self.request.user
似乎perform_create
方法不再存在或被调用,因此owner
永远不会被设置,因此我们看到了错误。我不确定我所做的是正确解决问题的方法,但它似乎有效!
这里是指向上述内容的文档的链接:http://www.django-rest-framework.org/api-guide/generic-views/#genericapiview
答案 1 :(得分:3)
似乎答案取决于django rest框架版本。我现在使用的是3.2.4版本。如果您使用相同的基于版本和类的视图,则应该输入:
serializer.save(owner=self.request.user)
而不是
serializer.save()
函数pre_create()应该从SnippetList类中删除。
答案 2 :(得分:1)
您可以在SnippedList的post方法中更新save()参数:
def post(self, request, format=None):
serializer = SnippetSerializer(data=request.data)
if serializer.is_valid():
serializer.save(owner=self.request.user)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
答案 3 :(得分:0)
perform_create位于CreateModelMixin中,您的视图应该是generics.ListCreateAPIView的子类