Django error – matching query does not exist


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/", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

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

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

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", 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).

Answered By – christophe31

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published