|
1 """ |
|
2 35. DB-API Shortcuts |
|
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. |
|
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. |
|
11 """ |
|
12 |
|
13 from django.db import models |
|
14 from django.http import Http404 |
|
15 from django.shortcuts import get_object_or_404, get_list_or_404 |
|
16 |
|
17 class Author(models.Model): |
|
18 name = models.CharField(maxlength=50) |
|
19 |
|
20 def __str__(self): |
|
21 return self.name |
|
22 |
|
23 class ArticleManager(models.Manager): |
|
24 def get_query_set(self): |
|
25 return super(ArticleManager, self).get_query_set().filter(authors__name__icontains='sir') |
|
26 |
|
27 class Article(models.Model): |
|
28 authors = models.ManyToManyField(Author) |
|
29 title = models.CharField(maxlength=50) |
|
30 objects = models.Manager() |
|
31 by_a_sir = ArticleManager() |
|
32 |
|
33 def __str__(self): |
|
34 return self.title |
|
35 |
|
36 __test__ = {'API_TESTS':""" |
|
37 # Create some Authors. |
|
38 >>> a = Author.objects.create(name="Brave Sir Robin") |
|
39 >>> a.save() |
|
40 >>> a2 = Author.objects.create(name="Patsy") |
|
41 >>> a2.save() |
|
42 |
|
43 # No Articles yet, so we should get a Http404 error. |
|
44 >>> get_object_or_404(Article, title="Foo") |
|
45 Traceback (most recent call last): |
|
46 ... |
|
47 Http404: No Article matches the given query. |
|
48 |
|
49 # Create an Article. |
|
50 >>> article = Article.objects.create(title="Run away!") |
|
51 >>> article.authors = [a, a2] |
|
52 >>> article.save() |
|
53 |
|
54 # get_object_or_404 can be passed a Model to query. |
|
55 >>> get_object_or_404(Article, title__contains="Run") |
|
56 <Article: Run away!> |
|
57 |
|
58 # We can also use the the Article manager through an Author object. |
|
59 >>> get_object_or_404(a.article_set, title__contains="Run") |
|
60 <Article: Run away!> |
|
61 |
|
62 # No articles containing "Camelot". This should raise a Http404 error. |
|
63 >>> get_object_or_404(a.article_set, title__contains="Camelot") |
|
64 Traceback (most recent call last): |
|
65 ... |
|
66 Http404: No Article matches the given query. |
|
67 |
|
68 # Custom managers can be used too. |
|
69 >>> get_object_or_404(Article.by_a_sir, title="Run away!") |
|
70 <Article: Run away!> |
|
71 |
|
72 # get_list_or_404 can be used to get lists of objects |
|
73 >>> get_list_or_404(a.article_set, title__icontains='Run') |
|
74 [<Article: Run away!>] |
|
75 |
|
76 # Http404 is returned if the list is empty |
|
77 >>> get_list_or_404(a.article_set, title__icontains='Shrubbery') |
|
78 Traceback (most recent call last): |
|
79 ... |
|
80 Http404: No Article matches the given query. |
|
81 |
|
82 # Custom managers can be used too. |
|
83 >>> get_list_or_404(Article.by_a_sir, title__icontains="Run") |
|
84 [<Article: Run away!>] |
|
85 |
|
86 """} |