equal
deleted
inserted
replaced
|
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 |
|
6 ``DoesNotExist`` exception was raised 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 |
|
10 ``DoesNotExist`` exception was raised 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(max_length=50) |
|
19 |
|
20 def __unicode__(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(max_length=50) |
|
30 objects = models.Manager() |
|
31 by_a_sir = ArticleManager() |
|
32 |
|
33 def __unicode__(self): |
|
34 return self.title |