Fixed #29148 -- Doc'd how to use get_or_create() with Q objects.
This commit is contained in:
parent
1bf4646f91
commit
34c5222837
|
@ -1857,8 +1857,20 @@ The above example can be rewritten using ``get_or_create()`` like so::
|
||||||
|
|
||||||
Any keyword arguments passed to ``get_or_create()`` — *except* an optional one
|
Any keyword arguments passed to ``get_or_create()`` — *except* an optional one
|
||||||
called ``defaults`` — will be used in a :meth:`get()` call. If an object is
|
called ``defaults`` — will be used in a :meth:`get()` call. If an object is
|
||||||
found, ``get_or_create()`` returns a tuple of that object and ``False``. If
|
found, ``get_or_create()`` returns a tuple of that object and ``False``.
|
||||||
multiple objects are found, ``get_or_create`` raises
|
|
||||||
|
You can specify more complex conditions for the retrieved object by chaining
|
||||||
|
``get_or_create()`` with ``filter()`` and using :class:`Q objects
|
||||||
|
<django.db.models.Q>`. For example, to retrieve Robert or Bob Marley if either
|
||||||
|
exists, and create the latter otherwise::
|
||||||
|
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
|
obj, created = Person.objects.filter(
|
||||||
|
Q(first_name='Bob') | Q(first_name='Robert'),
|
||||||
|
).get_or_create(last_name='Marley', defaults={'first_name': 'Bob'})
|
||||||
|
|
||||||
|
If multiple objects are found, ``get_or_create()`` raises
|
||||||
:exc:`~django.core.exceptions.MultipleObjectsReturned`. If an object is *not*
|
:exc:`~django.core.exceptions.MultipleObjectsReturned`. If an object is *not*
|
||||||
found, ``get_or_create()`` will instantiate and save a new object, returning a
|
found, ``get_or_create()`` will instantiate and save a new object, returning a
|
||||||
tuple of the new object and ``True``. The new object will be created roughly
|
tuple of the new object and ``True``. The new object will be created roughly
|
||||||
|
|
Loading…
Reference in New Issue