models.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  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.utils.encoding import python_2_unicode_compatible
  12. @python_2_unicode_compatible
  13. class Author(models.Model):
  14. name = models.CharField(max_length=50)
  15. def __str__(self):
  16. return self.name
  17. class ArticleManager(models.Manager):
  18. def get_queryset(self):
  19. return super(ArticleManager, self).get_queryset().filter(authors__name__icontains='sir')
  20. @python_2_unicode_compatible
  21. class Article(models.Model):
  22. authors = models.ManyToManyField(Author)
  23. title = models.CharField(max_length=50)
  24. objects = models.Manager()
  25. by_a_sir = ArticleManager()
  26. def __str__(self):
  27. return self.title