在为视图添加权限后,它会说创建几个片段.我通过可浏览的API登录并尝试发布这样的代码段:
{"Title": "snippet","code": "print 123"}
当我这样做时,我得到:
/ snippets /中的IntegrityError
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,**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,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解决方法 我今晚正在阅读本教程并遇到同样的错误.看来教程是一个版本左右的实际框架可能呢?无论如何,经过一些研究后,我能够获得片段保存.
教程说这样做的地方:
def perform_create(self,serializer): serializer.save(owner=self.request.user)
我改用这个:
def pre_save(self,snip): snip.owner = self.request.user
似乎perform_create方法不再存在或被调用,因此所有者永远不会被设置,因此我们看到了错误.我不确定我所做的是解决问题的正确方法,但似乎有效!
这是我在上面提到的文档的链接:http://www.django-rest-framework.org/api-guide/generic-views/#genericapiview
总结以上是内存溢出为你收集整理的django – Rest Framework教程IntegrityError创建片段全部内容,希望文章能够帮你解决django – Rest Framework教程IntegrityError创建片段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)