Django

Code

Changeset 4275

Show
Ignore:
Timestamp:
01/03/07 08:16:58 (2 years ago)
Author:
russellm
Message:

Fixed #2756 -- Modified the get_object_or_404/get_list_or_404 shortcuts to accept model managers as well as model classes. Thanks, Gary Wilson.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/shortcuts/__init__.py

    r3015 r4275  
    55from django.template import loader 
    66from django.http import HttpResponse, Http404 
    7  
     7from django.db.models.manager import Manager 
    88 
    99def render_to_response(*args, **kwargs): 
     
    1212 
    1313def 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 
    1419    try: 
    15         return klass._default_manager.get(*args, **kwargs) 
     20        return manager.get(*args, **kwargs) 
    1621    except klass.DoesNotExist: 
    1722        raise Http404 
    1823 
    1924def 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)) 
    2130    if not obj_list: 
    2231        raise Http404 
  • django/trunk/docs/db-api.txt

    r4221 r4275  
    17051705returns the height (or width) of the image, as an integer, in pixels. 
    17061706 
     1707Shortcuts 
     1708========= 
     1709 
     1710As you develop views, you will discover a number of common idioms in the 
     1711way you use the database API. Django encodes some of these idioms as 
     1712shortcuts that can be used to simplify the process of writing views. 
     1713 
     1714get_object_or_404() 
     1715------------------- 
     1716 
     1717One common idiom to use ``get()`` and raise ``Http404`` if the 
     1718object doesn't exist. This idiom is captured by ``get_object_or_404()``.  
     1719This function takes a Django model as its first argument and an  
     1720arbitrary number of keyword arguments, which it passes to the manager's  
     1721``get()`` function. It raises ``Http404`` if the object doesn't 
     1722exist. For example::  
     1723     
     1724    # Get the Entry with a primary key of 3 
     1725    e = get_object_or_404(Entry, pk=3) 
     1726 
     1727When you provide a model to this shortcut function, the default manager  
     1728is used to execute the underlying ``get()`` query. If you don't want to  
     1729use the default manager, or you want to search a list of related objects,  
     1730you can provide ``get_object_or_404()`` with a manager object, instead.  
     1731For 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 
     1740get_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 
    17071747Falling back to raw SQL 
    17081748======================= 
  • django/trunk/docs/tutorial03.txt

    r3912 r4275  
    301301The ``get_object_or_404()`` function takes a Django model module as its first 
    302302argument 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't 
     303module's ``get()`` function. It raises ``Http404`` if the object doesn't 
    304304exist. 
    305305