Changeset 4275
- Timestamp:
- 01/03/07 08:16:58 (2 years ago)
- Files:
-
- django/trunk/django/shortcuts/__init__.py (modified) (2 diffs)
- django/trunk/docs/db-api.txt (modified) (1 diff)
- django/trunk/docs/tutorial03.txt (modified) (1 diff)
- django/trunk/tests/modeltests/get_object_or_404 (added)
- django/trunk/tests/modeltests/get_object_or_404/__init__.py (added)
- django/trunk/tests/modeltests/get_object_or_404/models.py (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/shortcuts/__init__.py
r3015 r4275 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 8 9 9 def render_to_response(*args, **kwargs): … … 12 12 13 13 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 14 19 try: 15 return klass._default_manager.get(*args, **kwargs)20 return manager.get(*args, **kwargs) 16 21 except klass.DoesNotExist: 17 22 raise Http404 18 23 19 24 def get_list_or_404(klass, *args, **kwargs): 20 obj_list = list(klass._default_manager.filter(*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)) 21 30 if not obj_list: 22 31 raise Http404 django/trunk/docs/db-api.txt
r4221 r4275 1705 1705 returns the height (or width) of the image, as an integer, in pixels. 1706 1706 1707 Shortcuts 1708 ========= 1709 1710 As you develop views, you will discover a number of common idioms in the 1711 way you use the database API. Django encodes some of these idioms as 1712 shortcuts that can be used to simplify the process of writing views. 1713 1714 get_object_or_404() 1715 ------------------- 1716 1717 One common idiom to use ``get()`` and raise ``Http404`` if the 1718 object doesn't exist. This idiom is captured by ``get_object_or_404()``. 1719 This function takes a Django model as its first argument and an 1720 arbitrary number of keyword arguments, which it passes to the manager's 1721 ``get()`` function. It raises ``Http404`` if the object doesn't 1722 exist. For example:: 1723 1724 # Get the Entry with a primary key of 3 1725 e = get_object_or_404(Entry, pk=3) 1726 1727 When you provide a model to this shortcut function, the default manager 1728 is used to execute the underlying ``get()`` query. If you don't want to 1729 use the default manager, or you want to search a list of related objects, 1730 you can provide ``get_object_or_404()`` with a manager object, instead. 1731 For example:: 1732 1733 # Get the author of blog instance `e` with a name of 'Fred' 1734 a = get_object_or_404(e.authors, name='Fred') 1735 1736 # Use a custom manager 'recent_entries' in the search for an 1737 # entry with a primary key of 3 1738 e = get_object_or_404(Entry.recent_entries, pk=3) 1739 1740 get_list_or_404() 1741 ----------------- 1742 1743 ``get_list_or_404`` behaves the same was as ``get_object_or_404()`` 1744 -- except the it uses using ``filter()`` instead of ``get()``. It raises 1745 ``Http404`` if the list is empty. 1746 1707 1747 Falling back to raw SQL 1708 1748 ======================= django/trunk/docs/tutorial03.txt
r3912 r4275 301 301 The ``get_object_or_404()`` function takes a Django model module as its first 302 302 argument and an arbitrary number of keyword arguments, which it passes to the 303 module's ``get _object()`` function. It raises ``Http404`` if the object doesn't303 module's ``get()`` function. It raises ``Http404`` if the object doesn't 304 304 exist. 305 305
