Ticket #2756: shortcuts2.diff

File shortcuts2.diff, 3.0 KB (added by Gary Wilson <gary.wilson@…>, 9 years ago)

added modeltests for get_object_or_404

  • tests/modeltests/get_object_or_404/models.py

    === added directory 'tests/modeltests/get_object_or_404'
    === added file 'tests/modeltests/get_object_or_404/__init__.py'
    === added file 'tests/modeltests/get_object_or_404/models.py'
     
     1"""
     236. get_object_or_404()
     3
     4get_object_or_404 is a shortcut function to be used in view functions for
     5performing a get() lookup and raising a Http404 exception if a DoesNotExist
     6exception was rasied during the get() call.
     7"""
     8
     9from django.db import models
     10from django.http import Http404
     11from django.shortcuts import get_object_or_404
     12
     13class Author(models.Model):
     14    name = models.CharField(maxlength=50)
     15   
     16    def __str__(self):
     17        return self.name
     18
     19class Article(models.Model):
     20    author = models.ForeignKey(Author)
     21    title = models.CharField(maxlength=50)
     22   
     23    def __str__(self):
     24        return "%s by %s" % (self.title, self.author)
     25
     26__test__ = {'API_TESTS':"""
     27# Let's create an Author.
     28>>> a = Author.objects.create(name="Brave Sir Robin")
     29
     30# No Articles yet so we should get a Http404 error.
     31>>> get_object_or_404(Article, title="Foo") == Http404
     32Traceback (most recent call last):
     33...
     34Http404
     35
     36# Let's create an Article.
     37>>> article = Article.objects.create(author=a, title="Run away!")
     38
     39# get_object_or_404 can be passed a Model to query.
     40>>> get_object_or_404(Article, title__contains="Run")
     41<Article: Run away! by Brave Sir Robin>
     42
     43# We can also use the the Article manager through an Author object.
     44>>> get_object_or_404(a.article_set, title__contains="Run")
     45<Article: Run away! by Brave Sir Robin>
     46
     47# This should raise a Http404 error.
     48>>> get_object_or_404(a.article_set, title__contains="Camelot")
     49Traceback (most recent call last):
     50...
     51Http404
     52"""}
  • django/shortcuts/__init__.py

    === modified file 'django/shortcuts/__init__.py'
     
    44
    55from django.template import loader
    66from django.http import HttpResponse, Http404
     7from django.db.models.manager import Manager
    78
    89
    910def render_to_response(*args, **kwargs):
     
    1112load_and_render = render_to_response # For backwards compatibility.
    1213
    1314def get_object_or_404(klass, *args, **kwargs):
     15    if isinstance(klass, Manager):
     16        manager = klass
     17        klass = manager.model
     18    else:
     19        manager = klass._default_manager
    1420    try:
    15         return klass._default_manager.get(*args, **kwargs)
     21        return manager.get(*args, **kwargs)
    1622    except klass.DoesNotExist:
    1723        raise Http404
    1824
    1925def get_list_or_404(klass, *args, **kwargs):
    20     obj_list = list(klass._default_manager.filter(*args, **kwargs))
     26    if isinstance(klass, Manager):
     27        manager = klass
     28    else:
     29        manager = klass._default_manager
     30    obj_list = list(manager.filter(*args, **kwargs))
    2131    if not obj_list:
    2232        raise Http404
    2333    return obj_list
Back to Top