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