/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. """
  2. 35. DB-API Shortcuts
  3. ``get_object_or_404()`` is a shortcut function to be used in view functions for
  4. performing a ``get()`` lookup and raising a ``Http404`` exception if a
  5. ``DoesNotExist`` exception was raised during the ``get()`` call.
  6. ``get_list_or_404()`` is a shortcut function to be used in view functions for
  7. performing a ``filter()`` lookup and raising a ``Http404`` exception if a
  8. ``DoesNotExist`` exception was raised during the ``filter()`` call.
  9. """
  10. from django.db import models
  11. from django.http import Http404
  12. from django.shortcuts import get_object_or_404, get_list_or_404
  13. class Author(models.Model):
  14. name = models.CharField(max_length=50)
  15. def __unicode__(self):
  16. return self.name
  17. class ArticleManager(models.Manager):
  18. def get_query_set(self):
  19. return super(ArticleManager, self).get_query_set().filter(authors__name__icontains='sir')
  20. class Article(models.Model):
  21. authors = models.ManyToManyField(Author)
  22. title = models.CharField(max_length=50)
  23. objects = models.Manager()
  24. by_a_sir = ArticleManager()
  25. def __unicode__(self):
  26. return self.title