~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to tests/modeltests/get_object_or_404/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""
2
2
35. DB-API Shortcuts
3
3
 
4
 
get_object_or_404 is a shortcut function to be used in view functions for
5
 
performing a get() lookup and raising a Http404 exception if a DoesNotExist
6
 
exception was rasied during the get() call.
 
4
``get_object_or_404()`` is a shortcut function to be used in view functions for
 
5
performing a ``get()`` lookup and raising a ``Http404`` exception if a
 
6
``DoesNotExist`` exception was raised during the ``get()`` call.
7
7
 
8
 
get_list_or_404 is a shortcut function to be used in view functions for
9
 
performing a filter() lookup and raising a Http404 exception if a DoesNotExist
10
 
exception was rasied during the filter() call.
 
8
``get_list_or_404()`` is a shortcut function to be used in view functions for
 
9
performing a ``filter()`` lookup and raising a ``Http404`` exception if a
 
10
``DoesNotExist`` exception was raised during the ``filter()`` call.
11
11
"""
12
12
 
13
13
from django.db import models
15
15
from django.shortcuts import get_object_or_404, get_list_or_404
16
16
 
17
17
class Author(models.Model):
18
 
    name = models.CharField(maxlength=50)
19
 
    
20
 
    def __str__(self):
 
18
    name = models.CharField(max_length=50)
 
19
 
 
20
    def __unicode__(self):
21
21
        return self.name
22
22
 
23
23
class ArticleManager(models.Manager):
26
26
 
27
27
class Article(models.Model):
28
28
    authors = models.ManyToManyField(Author)
29
 
    title = models.CharField(maxlength=50)
 
29
    title = models.CharField(max_length=50)
30
30
    objects = models.Manager()
31
31
    by_a_sir = ArticleManager()
32
 
    
33
 
    def __str__(self):
 
32
 
 
33
    def __unicode__(self):
34
34
        return self.title
35
35
 
36
36
__test__ = {'API_TESTS':"""
69
69
>>> get_object_or_404(Article.by_a_sir, title="Run away!")
70
70
<Article: Run away!>
71
71
 
 
72
# QuerySets can be used too.
 
73
>>> get_object_or_404(Article.objects.all(), title__contains="Run")
 
74
<Article: Run away!>
 
75
 
 
76
# Just as when using a get() lookup, you will get an error if more than one
 
77
# object is returned.
 
78
>>> get_object_or_404(Author.objects.all())
 
79
Traceback (most recent call last):
 
80
...
 
81
MultipleObjectsReturned: get() returned more than one Author -- it returned ...! Lookup parameters were {}
 
82
 
 
83
# Using an EmptyQuerySet raises a Http404 error.
 
84
>>> get_object_or_404(Article.objects.none(), title__contains="Run")
 
85
Traceback (most recent call last):
 
86
...
 
87
Http404: No Article matches the given query.
 
88
 
72
89
# get_list_or_404 can be used to get lists of objects
73
90
>>> get_list_or_404(a.article_set, title__icontains='Run')
74
91
[<Article: Run away!>]
75
92
 
76
 
# Http404 is returned if the list is empty
 
93
# Http404 is returned if the list is empty.
77
94
>>> get_list_or_404(a.article_set, title__icontains='Shrubbery')
78
95
Traceback (most recent call last):
79
96
...
83
100
>>> get_list_or_404(Article.by_a_sir, title__icontains="Run")
84
101
[<Article: Run away!>]
85
102
 
 
103
# QuerySets can be used too.
 
104
>>> get_list_or_404(Article.objects.all(), title__icontains="Run")
 
105
[<Article: Run away!>]
 
106
 
86
107
"""}