Ticket #7840: get_list_or_404_r7997.patch

File get_list_or_404_r7997.patch, 1.2 KB (added by spike, 7 years ago)
  • django/shortcuts/__init__.py

     
    5151    """
    5252    Uses filter() to return a list of objects, or raise a Http404 exception if
    5353    the list is empty.
    54 
     54   
    5555    klass may be a Model, Manager, or QuerySet object. All other passed
    5656    arguments and keyword arguments are used in the filter() query.
    5757    """
    58     queryset = _get_queryset(klass)
    59     obj_list = list(queryset.filter(*args, **kwargs))
    60     if not obj_list:
     58    queryset = _get_queryset(klass).filter(*args, **kwargs)
     59
     60    class WrapperQuerySet (QuerySet) :
     61        def __getitem__ (self, slice) :
     62            r = super(WrapperQuerySet, self).__getitem__(slice)
     63            if len(r) < 1 :
     64                raise Http404('No %s matches the given query.' % self.model._meta.object_name)
     65
     66            return r
     67
     68    if queryset.count() < 1 :
    6169        raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
    62     return obj_list
     70
     71    queryset = queryset._clone(klass=WrapperQuerySet)
     72    return queryset
Back to Top