gpt4 book ai didi

django - Rest Framework 教程 IntegrityError 创建片段

转载 作者:行者123 更新时间:2023-12-01 23:03:59 25 4
gpt4 key购买 nike

我正在学习 Django Rest Framework 教程,但我被困在第 4 部分。

在为 View 添加权限后,它说要创建几个片段。我通过可浏览的 API 登录并尝试发布这样的片段:

{
"title": "snippet",
"code": "print 123"
}

当我这样做时:

/snippets/处的完整性错误

NOT 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

最佳答案

似乎答案取决于 Django REST 框架版本。下面的答案已经在 3.2.4 和 3.10.3 版本上进行了测试。如果您使用这些版本和基于类的 View ,您应该输入:

serializer.save(owner=self.request.user)

代替
serializer.save()

在 SnippetList.post() 方法中,使其如下所示:
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)

函数 pre_create() 应该从 SnippetList 类中排除。

关于django - Rest Framework 教程 IntegrityError 创建片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27138601/

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