mirror of
https://github.com/django/django.git
synced 2025-04-26 10:14:36 +00:00
Raising DatabaseError directly made it harder than it should to differentiate between IntegrityError when a forced update resulted in no affected rows. Introducing a specialized exception allows for callers to more easily silence, log, or turn them update failures into user facing exceptions (e.g. 404s). Thanks Mariusz for the review.
82 lines
2.9 KiB
Plaintext
82 lines
2.9 KiB
Plaintext
=====================
|
|
Model class reference
|
|
=====================
|
|
|
|
.. currentmodule:: django.db.models
|
|
|
|
This document covers features of the :class:`~django.db.models.Model` class.
|
|
For more information about models, see :doc:`the complete list of Model
|
|
reference guides </ref/models/index>`.
|
|
|
|
Attributes
|
|
==========
|
|
|
|
``DoesNotExist``
|
|
----------------
|
|
|
|
.. exception:: Model.DoesNotExist
|
|
|
|
This exception is raised by the ORM when an expected object is not found.
|
|
For example, :meth:`.QuerySet.get` will raise it when no object is found
|
|
for the given lookups.
|
|
|
|
Django provides a ``DoesNotExist`` exception as an attribute of each model
|
|
class to identify the class of object that could not be found, allowing you
|
|
to catch exceptions for a particular model class. The exception is a
|
|
subclass of :exc:`django.core.exceptions.ObjectDoesNotExist`.
|
|
|
|
``MultipleObjectsReturned``
|
|
---------------------------
|
|
|
|
.. exception:: Model.MultipleObjectsReturned
|
|
|
|
This exception is raised by :meth:`.QuerySet.get` when multiple objects are
|
|
found for the given lookups.
|
|
|
|
Django provides a ``MultipleObjectsReturned`` exception as an attribute of
|
|
each model class to identify the class of object for which multiple objects
|
|
were found, allowing you to catch exceptions for a particular model class.
|
|
The exception is a subclass of
|
|
:exc:`django.core.exceptions.MultipleObjectsReturned`.
|
|
|
|
``NotUpdated``
|
|
--------------
|
|
|
|
.. versionadded:: 6.0
|
|
|
|
.. exception:: Model.NotUpdated
|
|
|
|
This exception is raised when :ref:`a forced update
|
|
<ref-models-force-insert>` of a :class:`~django.db.models.Model` instance
|
|
does not affect any rows.
|
|
|
|
Django provides a ``NotUpdated`` exception as an attribute of each model
|
|
class to identify the class of object that could not be updated, allowing
|
|
you to catch exceptions for a particular model class. The exception is a
|
|
subclass of :exc:`django.core.exceptions.ObjectNotUpdated` and inherits
|
|
from :exc:`django.db.DatabaseError` for backward compatibility reasons.
|
|
|
|
``objects``
|
|
-----------
|
|
|
|
.. attribute:: Model.objects
|
|
|
|
Each non-abstract :class:`~django.db.models.Model` class must have a
|
|
:class:`~django.db.models.Manager` instance added to it.
|
|
Django ensures that in your model class you have at least a
|
|
default ``Manager`` specified. If you don't add your own ``Manager``,
|
|
Django will add an attribute ``objects`` containing default
|
|
:class:`~django.db.models.Manager` instance. If you add your own
|
|
:class:`~django.db.models.Manager` instance attribute, the default one does
|
|
not appear. Consider the following example::
|
|
|
|
from django.db import models
|
|
|
|
|
|
class Person(models.Model):
|
|
# Add manager with another name
|
|
people = models.Manager()
|
|
|
|
For more details on model managers see :doc:`Managers </topics/db/managers>`
|
|
and :ref:`Retrieving objects <retrieving-objects>`.
|