class.txt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. ``NotUpdated``
  31. --------------
  32. .. versionadded:: 6.0
  33. .. exception:: Model.NotUpdated
  34. This exception is raised when :ref:`a forced update
  35. <ref-models-force-insert>` of a :class:`~django.db.models.Model` instance
  36. does not affect any rows.
  37. Django provides a ``NotUpdated`` exception as an attribute of each model
  38. class to identify the class of object that could not be updated, allowing
  39. you to catch exceptions for a particular model class. The exception is a
  40. subclass of :exc:`django.core.exceptions.ObjectNotUpdated` and inherits
  41. from :exc:`django.db.DatabaseError` for backward compatibility reasons.
  42. ``objects``
  43. -----------
  44. .. attribute:: Model.objects
  45. Each non-abstract :class:`~django.db.models.Model` class must have a
  46. :class:`~django.db.models.Manager` instance added to it.
  47. Django ensures that in your model class you have at least a
  48. default ``Manager`` specified. If you don't add your own ``Manager``,
  49. Django will add an attribute ``objects`` containing default
  50. :class:`~django.db.models.Manager` instance. If you add your own
  51. :class:`~django.db.models.Manager` instance attribute, the default one does
  52. not appear. Consider the following example::
  53. from django.db import models
  54. class Person(models.Model):
  55. # Add manager with another name
  56. people = models.Manager()
  57. For more details on model managers see :doc:`Managers </topics/db/managers>`
  58. and :ref:`Retrieving objects <retrieving-objects>`.