mirror of https://github.com/django/django.git
Fixed #33298 -- Added docs and tests for using Q objects with get_object_or_404()/get_list_or_404().
This commit is contained in:
parent
ddf321479b
commit
7f8f69fb38
|
@ -157,8 +157,8 @@ will be returned::
|
||||||
but it raises :class:`~django.http.Http404` instead of the model's
|
but it raises :class:`~django.http.Http404` instead of the model's
|
||||||
:class:`~django.db.models.Model.DoesNotExist` exception.
|
:class:`~django.db.models.Model.DoesNotExist` exception.
|
||||||
|
|
||||||
Required arguments
|
Arguments
|
||||||
------------------
|
---------
|
||||||
|
|
||||||
``klass``
|
``klass``
|
||||||
A :class:`~django.db.models.Model` class,
|
A :class:`~django.db.models.Model` class,
|
||||||
|
@ -166,6 +166,9 @@ Required arguments
|
||||||
or a :class:`~django.db.models.query.QuerySet` instance from which to get
|
or a :class:`~django.db.models.query.QuerySet` instance from which to get
|
||||||
the object.
|
the object.
|
||||||
|
|
||||||
|
``*args``
|
||||||
|
:class:`Q objects <django.db.models.Q>`.
|
||||||
|
|
||||||
``**kwargs``
|
``**kwargs``
|
||||||
Lookup parameters, which should be in the format accepted by ``get()`` and
|
Lookup parameters, which should be in the format accepted by ``get()`` and
|
||||||
``filter()``.
|
``filter()``.
|
||||||
|
@ -230,14 +233,17 @@ will be raised if more than one object is found.
|
||||||
given model manager cast to a list, raising :class:`~django.http.Http404` if
|
given model manager cast to a list, raising :class:`~django.http.Http404` if
|
||||||
the resulting list is empty.
|
the resulting list is empty.
|
||||||
|
|
||||||
Required arguments
|
Arguments
|
||||||
------------------
|
---------
|
||||||
|
|
||||||
``klass``
|
``klass``
|
||||||
A :class:`~django.db.models.Model`, :class:`~django.db.models.Manager` or
|
A :class:`~django.db.models.Model`, :class:`~django.db.models.Manager` or
|
||||||
:class:`~django.db.models.query.QuerySet` instance from which to get the
|
:class:`~django.db.models.query.QuerySet` instance from which to get the
|
||||||
list.
|
list.
|
||||||
|
|
||||||
|
``*args``
|
||||||
|
:class:`Q objects <django.db.models.Q>`.
|
||||||
|
|
||||||
``**kwargs``
|
``**kwargs``
|
||||||
Lookup parameters, which should be in the format accepted by ``get()`` and
|
Lookup parameters, which should be in the format accepted by ``get()`` and
|
||||||
``filter()``.
|
``filter()``.
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from django.db.models import Q
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.shortcuts import get_list_or_404, get_object_or_404
|
from django.shortcuts import get_list_or_404, get_object_or_404
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
@ -75,6 +76,23 @@ class GetObjectOr404Tests(TestCase):
|
||||||
get_list_or_404(Article.objects.all(), title__icontains="Run"),
|
get_list_or_404(Article.objects.all(), title__icontains="Run"),
|
||||||
[article]
|
[article]
|
||||||
)
|
)
|
||||||
|
# Q objects.
|
||||||
|
self.assertEqual(
|
||||||
|
get_object_or_404(
|
||||||
|
Article,
|
||||||
|
Q(title__startswith='Run') | Q(title__startswith='Walk'),
|
||||||
|
authors__name__contains='Brave',
|
||||||
|
),
|
||||||
|
article,
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
get_list_or_404(
|
||||||
|
Article,
|
||||||
|
Q(title__startswith='Run') | Q(title__startswith='Walk'),
|
||||||
|
authors__name='Patsy',
|
||||||
|
),
|
||||||
|
[article],
|
||||||
|
)
|
||||||
|
|
||||||
def test_bad_class(self):
|
def test_bad_class(self):
|
||||||
# Given an argument klass that is not a Model, Manager, or Queryset
|
# Given an argument klass that is not a Model, Manager, or Queryset
|
||||||
|
|
Loading…
Reference in New Issue