Ticket #7840: get_list_or_404_r7997.patch
File get_list_or_404_r7997.patch, 1.2 KB (added by , 16 years ago) |
---|
-
django/shortcuts/__init__.py
51 51 """ 52 52 Uses filter() to return a list of objects, or raise a Http404 exception if 53 53 the list is empty. 54 54 55 55 klass may be a Model, Manager, or QuerySet object. All other passed 56 56 arguments and keyword arguments are used in the filter() query. 57 57 """ 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 : 61 69 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