我想创建“赞按钮”功能,但是,它仅在0 IntegrityError错误FOREIGN KEY约束在
处理上述异常(不存在匹配查询),
发生另一个异常: 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/base/base.py”
在_commit中
239. return self.connection.commit() 上面的异常(FOREIGN KEY约束失败)是直接的
导致以下异常的原因: 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/exception.py”
在内部
34. response = get_response(request) 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/base.py”
在_get_response中
126. response = self.process_exception_by_middleware(e,request) 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/base.py”
在_get_response中
124. response = wrapd_callback(request,* callback_args,** callback_kwargs) 文件“ /Users/mingselene/Documents/GitHub/network/feedback/views.py”位于
喜欢
42. new_like,created = Like.objects.get_or_create(user = request.user,post_id = post_id) 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/manager.py”
在manager_method中
82. return getattr(self.get_queryset(),name)(* args,** kwargs) 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py”
在get_or_create中
488. return self._create_object_from_params(lookup,params) 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py”
在_create_object_from_params中
530.提高e 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py”
在_create_object_from_params中
522. obj = self.create(** params) 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/transaction.py”
在退出中
212. connection.commit() 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/base/base.py”
提交中
261. self._commit() 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/base/base.py”
在_commit中
239. return self.connection.commit() 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/utils.py”
在退出中
89.从exc_value提高dj_exc_value.with_traceback(traceback) 文件
“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/base/base.py”
在_commit中
239. return self.connection.commit() 异常类型:/ likes / 8 /的IntegrityError异常值:FOREIGN
密钥约束失败 我不明白为什么会这样,并且stackoverflow帖子无效。
在/ likes / 8 /处的
views.py:
def likes(request, post_id):
post = Post.objects.get(id = post_id)
new_like, created = Like.objects.get_or_create(user=request.user, post_id=post_id)
if not created:
post.numOfLike -= 1
like = Like.objects.get(user=request.user, post_id=post_id)
like.delete()
created = True
else:
post.numOfLike += 1
post.save(update_fields=['numOfLike'])
return HttpResponseRedirect("/")
models.py:
class Like(models.Model):
post = models.ForeignKey('feedback.Subject', default=1, on_delete=models.CASCADE)
user = models.ForeignKey(User, default=1, on_delete=models.CASCADE,)
isUserLiked = models.DateTimeField(auto_now_add=True)"