| 1 |
""" |
| 2 |
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 |
|
| 15 |
|
| 16 |
class Author(models.Model): |
| 17 |
name = models.CharField(max_length=50) |
| 18 |
|
| 19 |
|
| 20 |
class ArticleManager(models.Manager): |
| 21 |
def get_queryset(self): |
| 22 |
return super().get_queryset().filter(authors__name__icontains="sir") |
| 23 |
|
| 24 |
|
| 25 |
class AttributeErrorManager(models.Manager): |
| 26 |
def get_queryset(self): |
| 27 |
raise AttributeError("AttributeErrorManager") |
| 28 |
|
| 29 |
|
| 30 |
class Article(models.Model): |
| 31 |
authors = models.ManyToManyField(Author) |
| 32 |
title = models.CharField(max_length=50) |
| 33 |
objects = models.Manager() |
| 34 |
by_a_sir = ArticleManager() |
| 35 |
attribute_error_objects = AttributeErrorManager() |