models.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. """
  2. 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. class Author(models.Model):
  12. name = models.CharField(max_length=50)
  13. class ArticleManager(models.Manager):
  14. def get_queryset(self):
  15. return super().get_queryset().filter(authors__name__icontains="sir")
  16. class AttributeErrorManager(models.Manager):
  17. def get_queryset(self):
  18. raise AttributeError("AttributeErrorManager")
  19. class Article(models.Model):
  20. authors = models.ManyToManyField(Author)
  21. title = models.CharField(max_length=50)
  22. objects = models.Manager()
  23. by_a_sir = ArticleManager()
  24. attribute_error_objects = AttributeErrorManager()