Ticket #2756: shortcuts3.diff

File shortcuts3.diff, 3.4 KB (added by Gary Wilson <gary.wilson@…>, 18 years ago)

added a test for custom manager

  • 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 ArticleManager(models.Manager):
     20    def get_query_set(self):
     21        return super(ArticleManager, self).get_query_set().filter(author__name__icontains='sir')
     22
     23class Article(models.Model):
     24    author = models.ForeignKey(Author)
     25    title = models.CharField(maxlength=50)
     26    objects = models.Manager()
     27    by_a_sir = ArticleManager()
     28   
     29    def __str__(self):
     30        return "%s by %s" % (self.title, self.author)
     31
     32__test__ = {'API_TESTS':"""
     33# Let's create an Author.
     34>>> a = Author.objects.create(name="Brave Sir Robin")
     35
     36# No Articles yet so we should get a Http404 error.
     37>>> get_object_or_404(Article, title="Foo") == Http404
     38Traceback (most recent call last):
     39...
     40Http404
     41
     42# Let's create an Article.
     43>>> article = Article.objects.create(author=a, title="Run away!")
     44
     45# get_object_or_404 can be passed a Model to query.
     46>>> get_object_or_404(Article, title__contains="Run")
     47<Article: Run away! by Brave Sir Robin>
     48
     49# We can also use the the Article manager through an Author object.
     50>>> get_object_or_404(a.article_set, title__contains="Run")
     51<Article: Run away! by Brave Sir Robin>
     52
     53# No articles containing "Camelot".  This should raise a Http404 error.
     54>>> get_object_or_404(a.article_set, title__contains="Camelot")
     55Traceback (most recent call last):
     56...
     57Http404
     58
     59# Custom managers can be used too.
     60>>> get_object_or_404(Article.by_a_sir, title="Run away!")
     61<Article: Run away! by Brave Sir Robin>
     62"""}
  • 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