Doc'd Model.MultipleObjectsReturned docs and improved documentation related with models exceptions.
This commit is contained in:
parent
b5f0efa19c
commit
bc4fea92b2
|
@ -26,12 +26,12 @@ Django core exception classes are defined in ``django.core.exceptions``.
|
||||||
|
|
||||||
.. exception:: ObjectDoesNotExist
|
.. exception:: ObjectDoesNotExist
|
||||||
|
|
||||||
The base class for :exc:`~django.db.models.Model.DoesNotExist` exceptions;
|
The base class for :exc:`Model.DoesNotExist
|
||||||
a ``try/except`` for ``ObjectDoesNotExist`` will catch
|
<django.db.models.Model.DoesNotExist>` exceptions. A ``try/except`` for
|
||||||
|
``ObjectDoesNotExist`` will catch
|
||||||
:exc:`~django.db.models.Model.DoesNotExist` exceptions for all models.
|
:exc:`~django.db.models.Model.DoesNotExist` exceptions for all models.
|
||||||
|
|
||||||
See :meth:`~django.db.models.query.QuerySet.get()` for further information
|
See :meth:`~django.db.models.query.QuerySet.get()`.
|
||||||
on :exc:`ObjectDoesNotExist` and :exc:`~django.db.models.Model.DoesNotExist`.
|
|
||||||
|
|
||||||
``EmptyResultSet``
|
``EmptyResultSet``
|
||||||
------------------
|
------------------
|
||||||
|
@ -56,13 +56,13 @@ Django core exception classes are defined in ``django.core.exceptions``.
|
||||||
|
|
||||||
.. exception:: MultipleObjectsReturned
|
.. exception:: MultipleObjectsReturned
|
||||||
|
|
||||||
The :exc:`MultipleObjectsReturned` exception is raised by a query if only
|
The base class for :exc:`Model.MultipleObjectsReturned
|
||||||
one object is expected, but multiple objects are returned. A base version
|
<django.db.models.Model.MultipleObjectsReturned>` exceptions. A
|
||||||
of this exception is provided in :mod:`django.core.exceptions`; each model
|
``try/except`` for ``MultipleObjectsReturned`` will catch
|
||||||
class contains a subclassed version that can be used to identify the
|
:exc:`~django.db.models.Model.MultipleObjectsReturned` exceptions for all
|
||||||
specific object type that has returned multiple objects.
|
models.
|
||||||
|
|
||||||
See :meth:`~django.db.models.query.QuerySet.get()` for further information.
|
See :meth:`~django.db.models.query.QuerySet.get()`.
|
||||||
|
|
||||||
``SuspiciousOperation``
|
``SuspiciousOperation``
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|
|
@ -25,6 +25,20 @@ Attributes
|
||||||
to catch exceptions for a particular model class. The exception is a
|
to catch exceptions for a particular model class. The exception is a
|
||||||
subclass of :exc:`django.core.exceptions.ObjectDoesNotExist`.
|
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`.
|
||||||
|
|
||||||
``objects``
|
``objects``
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
|
|
|
@ -1999,14 +1999,16 @@ your resulting ``User`` model will have the following attributes::
|
||||||
>>> hasattr(user, 'supervisor_of')
|
>>> hasattr(user, 'supervisor_of')
|
||||||
True
|
True
|
||||||
|
|
||||||
A ``DoesNotExist`` exception is raised when accessing the reverse relationship
|
A ``RelatedObjectDoesNotExist`` exception is raised when accessing the reverse
|
||||||
if an entry in the related table doesn't exist. For example, if a user doesn't
|
relationship if an entry in the related table doesn't exist. This is a subclass
|
||||||
have a supervisor designated by ``MySpecialUser``::
|
of the target model's :exc:`Model.DoesNotExist
|
||||||
|
<django.db.models.Model.DoesNotExist>` exception. For example, if a user
|
||||||
|
doesn't have a supervisor designated by ``MySpecialUser``::
|
||||||
|
|
||||||
>>> user.supervisor_of
|
>>> user.supervisor_of
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
DoesNotExist: User matching query does not exist.
|
RelatedObjectDoesNotExist: User has no supervisor_of.
|
||||||
|
|
||||||
.. _onetoone-arguments:
|
.. _onetoone-arguments:
|
||||||
|
|
||||||
|
|
|
@ -1844,34 +1844,42 @@ they query the database each time they're called.
|
||||||
.. method:: get(**kwargs)
|
.. method:: get(**kwargs)
|
||||||
|
|
||||||
Returns the object matching the given lookup parameters, which should be in
|
Returns the object matching the given lookup parameters, which should be in
|
||||||
the format described in `Field lookups`_.
|
the format described in `Field lookups`_. You should use lookups that are
|
||||||
|
guaranteed unique, such as the primary key or fields in a unique constraint.
|
||||||
|
For example::
|
||||||
|
|
||||||
``get()`` raises :exc:`~django.core.exceptions.MultipleObjectsReturned` if more
|
Entry.objects.get(id=1)
|
||||||
than one object was found. The
|
Entry.objects.get(blog=blog, entry_number=1)
|
||||||
:exc:`~django.core.exceptions.MultipleObjectsReturned` exception is an
|
|
||||||
attribute of the model class.
|
|
||||||
|
|
||||||
``get()`` raises a :exc:`~django.db.models.Model.DoesNotExist` exception if an
|
If you expect a queryset to already return one row, you can use ``get()``
|
||||||
object wasn't found for the given parameters. This exception is an attribute
|
without any arguments to return the object for that row::
|
||||||
of the model class. Example::
|
|
||||||
|
|
||||||
Entry.objects.get(id='foo') # raises Entry.DoesNotExist
|
Entry.objects.filter(pk=1).get()
|
||||||
|
|
||||||
The :exc:`~django.db.models.Model.DoesNotExist` exception inherits from
|
If ``get()`` doesn't find any object, it raises a :exc:`Model.DoesNotExist
|
||||||
:exc:`django.core.exceptions.ObjectDoesNotExist`, so you can target multiple
|
<django.db.models.Model.DoesNotExist>` exception::
|
||||||
:exc:`~django.db.models.Model.DoesNotExist` exceptions. Example::
|
|
||||||
|
Entry.objects.get(id=-999) # raises Entry.DoesNotExist
|
||||||
|
|
||||||
|
If ``get()`` finds more than one object, it raises a
|
||||||
|
:exc:`Model.MultipleObjectsReturned
|
||||||
|
<django.db.models.Model.MultipleObjectsReturned>` exception::
|
||||||
|
|
||||||
|
Entry.objects.get(name='A Duplicated Name') # raises Entry.MultipleObjectsReturned
|
||||||
|
|
||||||
|
Both these exception classes are attributes of the model class, and specific to
|
||||||
|
that model. If you want to handle such exceptions from several ``get()`` calls
|
||||||
|
for different models, you can use their generic base classes. For example, you
|
||||||
|
can use :exc:`django.core.exceptions.ObjectDoesNotExist` to handle
|
||||||
|
:exc:`~django.db.models.Model.DoesNotExist` exceptions from multiple models::
|
||||||
|
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
|
||||||
try:
|
try:
|
||||||
e = Entry.objects.get(id=3)
|
blog = Blog.objects.get(id=1)
|
||||||
b = Blog.objects.get(id=1)
|
entry = Entry.objects.get(blog=blog, entry_number=1)
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
print("Either the entry or blog doesn't exist.")
|
print("Either the blog or entry doesn't exist.")
|
||||||
|
|
||||||
If you expect a queryset to return one row, you can use ``get()`` without any
|
|
||||||
arguments to return the object for that row::
|
|
||||||
|
|
||||||
entry = Entry.objects.filter(...).exclude(...).get()
|
|
||||||
|
|
||||||
``create()``
|
``create()``
|
||||||
~~~~~~~~~~~~
|
~~~~~~~~~~~~
|
||||||
|
|
Loading…
Reference in New Issue