Ticket #4373: full.diff

File full.diff, 3.9 KB (added by SuperJared, 17 years ago)

Full diff including docs, tests and updated code

  • django/shortcuts/__init__.py

     
    55from django.template import loader
    66from django.http import HttpResponse, Http404
    77from django.db.models.manager import Manager
     8from django.db.models.query import QuerySet
    89
    910def render_to_response(*args, **kwargs):
    1011    return HttpResponse(loader.render_to_string(*args, **kwargs))
    1112load_and_render = render_to_response # For backwards compatibility.
    1213
     14def _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
    1328def 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)
    1930    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)
    2334
    2435def 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))
    3038    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)
    3240    return obj_list
  • tests/modeltests/get_object_or_404/models.py

     
    6969>>> get_object_or_404(Article.by_a_sir, title="Run away!")
    7070<Article: Run away!>
    7171
     72# QuerySets can be used too
     73>>> get_object_or_404(Article.objects.all(), title__contains="Run")
     74<Article: Run away!>
     75
    7276# get_list_or_404 can be used to get lists of objects
    7377>>> get_list_or_404(a.article_set, title__icontains='Run')
    7478[<Article: Run away!>]
     
    8387>>> get_list_or_404(Article.by_a_sir, title__icontains="Run")
    8488[<Article: Run away!>]
    8589
     90# QuerySets can be used too
     91>>> get_list_or_404(Article.objects.all(), title__icontains="Run")
     92[<Article: Run away!>]
     93
    8694"""}
  • docs/db-api.txt

     
    17661766When you provide a model to this shortcut function, the default manager
    17671767is used to execute the underlying ``get()`` query. If you don't want to
    17681768use 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.
     1769you can provide ``get_object_or_404()`` with a manager object or queryset instead.
    17701770For example::
    17711771
    17721772    # Get the author of blog instance `e` with a name of 'Fred'
     
    17751775    # Use a custom manager 'recent_entries' in the search for an
    17761776    # entry with a primary key of 3
    17771777    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)
    17781783
    17791784get_list_or_404()
    17801785-----------------
Back to Top