Django错误 - 匹配查询不存在 [英] Django error - matching query does not exist

查看:567
本文介绍了Django错误 - 匹配查询不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于把我的项目发布到了生产阶段,突然间我在开发阶段从未遇到过一些问题。



当用户发布一些动作我有时会收到以下错误。

 追溯(最近的最后一次通话):

文件 /usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py,第111行,get_response
response = callback(request,* callback_args,** callback_kwargs)

文件home / ubuntu / server / opineer / comments / views.py,第103行,comment_expand
comment = Comment.objects.get(pk = comment_id)

文件/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py,第131行,get
return self.get_query_set()。get(* args ,** kwargs)

文件/usr/local/lib/python2.7/dist-packages/django/db/models/query.py,第366行,get
%self.model._meta.object_name)

DoesNotExist:注释匹配查询不存在

真正令人沮丧的是,该项目在本地环境中工作正常,此外,匹配的查询对象DOES存在在数据库中。



现在我怀疑用户在保留给其他用户时正在访问数据库,但无法证明我的观点,也没有任何解决方案



以前有人有这样的问题吗?关于如何解决这个问题的任何建议?



提前非常感谢您的帮助。



编辑:我已经使用从我收到的服务器错误电子邮件中检索到的相同信息手动查询数据库。我没有任何问题就能打入这个条目。此外,似乎完全相同的行为,用户执行不会在大多数时间内引起任何问题,而是在某些(这是尚未知的)情况下。总而言之,这绝对不是数据库中缺少的条目的问题。

解决方案

你的行提出错误在这里:

  comment = Comment.objects.get(pk = comment_id)

访问不存在的注释。

  from django.shortcuts import get_object_or_404 

comment = get_object_or_404评论,pk = comment_id)

您的用户不会在您的服务器上发生错误,而是会获得404这意味着他试图访问一个不存在的资源。



确定这里我想你知道这一点。



有些用户(我也是其中的一部分)让标签片长时间运行,如果用户有权删除数据,可能会发生。一个404错误可能是处理已删除资源错误的更好的错误,而不是向管理员发送电子邮件。



其他用户从历史记录中转到地址(如果数据相同已被删除,因为它可能会发生)。


I finally released my project to the production level and suddenly I have some issues I never had to deal with in the development phase.

When the users posts some actions, I sometimes get the following error.

Traceback (most recent call last):

  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "home/ubuntu/server/opineer/comments/views.py", line 103, in comment_expand
    comment = Comment.objects.get(pk=comment_id)

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 131, in get
    return self.get_query_set().get(*args, **kwargs)

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 366, in get
    % self.model._meta.object_name)

DoesNotExist: Comment matching query does not exist

What really frustrates me is that the project works fine in the local environment and furthermore, the matching query object DOES exist in the Database.

Now I am suspecting that the user is accessing the Database when it's reserved to other users, but there's no way to prove my argument nor I have any solution to it.

Does anybody had this kind of issue before? Any suggestions on how to resolve this issue?

Thank you very much for your help in advance.

EDIT: I have manually queried the database using the same information retrieved from the server error email I received. I was able to hit the entry without any issue. Furthermore, it seems like the exact same behavior the user performed does not raise any issue most of the time, but rather in some (which is yet unknown) cases. In conclusion, it definitely is not an issue with the missing entry in the database.

解决方案

your line raising the error is here:

comment = Comment.objects.get(pk=comment_id)

you try to access a non-existing comment.

from django.shortcuts import get_object_or_404

comment = get_object_or_404(Comment, pk=comment_id)

Instead of having an error on your server, your user will get a 404 meaning that he tries to access a non existing resource.

Ok up to here I suppose you are aware of this.

Some users (and I'm part of them) let tabs running for long time, if users are authorized to delete data, it may happens. A 404 error may be a better error to handle a deleted resource error than sending an email to the admin.

Other users go to addresses from their history, (same if data have been deleted since it may happens).

这篇关于Django错误 - 匹配查询不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆