Ticket #13400: 13400-better-doesnotexist-error-message.diff

File 13400-better-doesnotexist-error-message.diff, 835 bytes (added by aaron, 5 years ago)

Patch to give better error message when QuerySet.get(...) raises model.DoesNotExist

  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 2bcaf98..96a1c2b 100644
    a b class QuerySet(object): 
    338338        if num == 1:
    339339            return clone._result_cache[0]
    340340        if not num:
    341             raise self.model.DoesNotExist("%s matching query does not exist."
    342                     % self.model._meta.object_name)
     341            raise self.model.DoesNotExist(
     342                "%s matching query does not exist.  "
     343                "get() parameters were %s"
     344                % (self.model._meta.object_name, kwargs)
     345            )
    343346        raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s"
    344347                % (self.model._meta.object_name, num, kwargs))
    345348
Back to Top