PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/get_object_or_404/models.py

https://code.google.com/p/mango-py/
Python | 34 lines | 17 code | 6 blank | 11 comment | 0 complexity | 653932d4c3f179878a87dd43fd9e9386 MD5 | raw file
 1"""
 235. DB-API Shortcuts
 3
 4``get_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
 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
 9performing a ``filter()`` lookup and raising a ``Http404`` exception if a
10``DoesNotExist`` exception was raised during the ``filter()`` call.
11"""
12
13from django.db import models
14from django.http import Http404
15from django.shortcuts import get_object_or_404, get_list_or_404
16
17class Author(models.Model):
18    name = models.CharField(max_length=50)
19
20    def __unicode__(self):
21        return self.name
22
23class ArticleManager(models.Manager):
24    def get_query_set(self):
25        return super(ArticleManager, self).get_query_set().filter(authors__name__icontains='sir')
26
27class 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