class.txt 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. =====================
  2. Model class reference
  3. =====================
  4. .. currentmodule:: django.db.models
  5. This document covers features of the :class:`~django.db.models.Model` class.
  6. For more information about models, see :doc:`the complete list of Model
  7. reference guides </ref/models/index>`.
  8. Attributes
  9. ==========
  10. ``DoesNotExist``
  11. ----------------
  12. .. exception:: Model.DoesNotExist
  13. This exception is raised by the ORM when an expected object is not found.
  14. For example, :meth:`.QuerySet.get` will raise it when no object is found
  15. for the given lookups.
  16. Django provides a ``DoesNotExist`` exception as an attribute of each model
  17. class to identify the class of object that could not be found, allowing you
  18. to catch exceptions for a particular model class. The exception is a
  19. subclass of :exc:`django.core.exceptions.ObjectDoesNotExist`.
  20. ``MultipleObjectsReturned``
  21. ---------------------------
  22. .. exception:: Model.MultipleObjectsReturned
  23. This exception is raised by :meth:`.QuerySet.get` when multiple objects are
  24. found for the given lookups.
  25. Django provides a ``MultipleObjectsReturned`` exception as an attribute of
  26. each model class to identify the class of object for which multiple objects
  27. were found, allowing you to catch exceptions for a particular model class.
  28. The exception is a subclass of
  29. :exc:`django.core.exceptions.MultipleObjectsReturned`.
  30. ``objects``
  31. -----------
  32. .. attribute:: Model.objects
  33. Each non-abstract :class:`~django.db.models.Model` class must have a
  34. :class:`~django.db.models.Manager` instance added to it.
  35. Django ensures that in your model class you have at least a
  36. default ``Manager`` specified. If you don't add your own ``Manager``,
  37. Django will add an attribute ``objects`` containing default
  38. :class:`~django.db.models.Manager` instance. If you add your own
  39. :class:`~django.db.models.Manager` instance attribute, the default one does
  40. not appear. Consider the following example::
  41. from django.db import models
  42. class Person(models.Model):
  43. # Add manager with another name
  44. people = models.Manager()
  45. For more details on model managers see :doc:`Managers </topics/db/managers>`
  46. and :ref:`Retrieving objects <retrieving-objects>`.