Ticket #4373: full.diff
File full.diff, 3.9 KB (added by , 17 years ago) |
---|
-
django/shortcuts/__init__.py
5 5 from django.template import loader 6 6 from django.http import HttpResponse, Http404 7 7 from django.db.models.manager import Manager 8 from django.db.models.query import QuerySet 8 9 9 10 def render_to_response(*args, **kwargs): 10 11 return HttpResponse(loader.render_to_string(*args, **kwargs)) 11 12 load_and_render = render_to_response # For backwards compatibility. 12 13 14 def _get_queryset(klass): 15 ''' 16 Returns a queryset from a Model, Manager or Queryset. Created to make 17 get_object_or_404 and get_list_or_404 more DRY. 18 ''' 19 if isinstance(klass, QuerySet): 20 return klass 21 else: 22 if isinstance(klass, Manager): 23 manager = klass 24 else: 25 manager = klass._default_manager 26 return manager.all() 27 13 28 def get_object_or_404(klass, *args, **kwargs): 14 if isinstance(klass, Manager): 15 manager = klass 16 klass = manager.model 17 else: 18 manager = klass._default_manager 29 queryset = _get_queryset(klass) 19 30 try: 20 return manager.get(*args, **kwargs)21 except klass.DoesNotExist:22 raise Http404('No %s matches the given query.' % klass._meta.object_name)31 return queryset.get(*args, **kwargs) 32 except queryset.model.DoesNotExist: 33 raise Http404('No %s matches the given query.' % queryset.model._meta.object_name) 23 34 24 35 def get_list_or_404(klass, *args, **kwargs): 25 if isinstance(klass, Manager): 26 manager = klass 27 else: 28 manager = klass._default_manager 29 obj_list = list(manager.filter(*args, **kwargs)) 36 queryset = _get_queryset(klass) 37 obj_list = list(queryset.filter(*args, **kwargs)) 30 38 if not obj_list: 31 raise Http404('No %s matches the given query.' % manager.model._meta.object_name)39 raise Http404('No %s matches the given query.' % queryset.model._meta.object_name) 32 40 return obj_list -
tests/modeltests/get_object_or_404/models.py
69 69 >>> get_object_or_404(Article.by_a_sir, title="Run away!") 70 70 <Article: Run away!> 71 71 72 # QuerySets can be used too 73 >>> get_object_or_404(Article.objects.all(), title__contains="Run") 74 <Article: Run away!> 75 72 76 # get_list_or_404 can be used to get lists of objects 73 77 >>> get_list_or_404(a.article_set, title__icontains='Run') 74 78 [<Article: Run away!>] … … 83 87 >>> get_list_or_404(Article.by_a_sir, title__icontains="Run") 84 88 [<Article: Run away!>] 85 89 90 # QuerySets can be used too 91 >>> get_list_or_404(Article.objects.all(), title__icontains="Run") 92 [<Article: Run away!>] 93 86 94 """} -
docs/db-api.txt
1766 1766 When you provide a model to this shortcut function, the default manager 1767 1767 is used to execute the underlying ``get()`` query. If you don't want to 1768 1768 use the default manager, or if you want to search a list of related objects, 1769 you can provide ``get_object_or_404()`` with a manager object instead.1769 you can provide ``get_object_or_404()`` with a manager object or queryset instead. 1770 1770 For example:: 1771 1771 1772 1772 # Get the author of blog instance `e` with a name of 'Fred' … … 1775 1775 # Use a custom manager 'recent_entries' in the search for an 1776 1776 # entry with a primary key of 3 1777 1777 e = get_object_or_404(Entry.recent_entries, pk=3) 1778 1779 # Use a QuerySet from a custom 'published' method in a 1780 # customized manager in the search for an entry with 1781 # primary key of 5 1782 e = get_object_or_404(Entry.objects.published(), pk=5) 1778 1783 1779 1784 get_list_or_404() 1780 1785 -----------------