1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #33298 -- Added docs and tests for using Q objects with get_object_or_404()/get_list_or_404().

This commit is contained in:
mgaligniana
2021-11-22 16:30:07 -03:00
committed by Mariusz Felisiak
parent ddf321479b
commit 7f8f69fb38
2 changed files with 28 additions and 4 deletions

View File

@@ -1,3 +1,4 @@
from django.db.models import Q
from django.http import Http404
from django.shortcuts import get_list_or_404, get_object_or_404
from django.test import TestCase
@@ -75,6 +76,23 @@ class GetObjectOr404Tests(TestCase):
get_list_or_404(Article.objects.all(), title__icontains="Run"),
[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):
# Given an argument klass that is not a Model, Manager, or Queryset