Skip to content

Commit f6625d9

Browse files
committed
Add related field test
1 parent ce4e4df commit f6625d9

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

tests/model_fields_/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,11 @@ class MuseumExhibit(models.Model):
194194

195195
def __str__(self):
196196
return self.exhibit_name
197+
198+
199+
class Tour(models.Model):
200+
guide = models.CharField(max_length=100)
201+
exhibit = models.ForeignKey(MuseumExhibit, on_delete=models.CASCADE)
202+
203+
def __str__(self):
204+
return f"Tour by {self.guide}"

tests/model_fields_/test_embedded_model_array.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
MuseumExhibit,
1616
RestorationRecord,
1717
Review,
18+
Tour,
1819
)
1920

2021

@@ -150,6 +151,9 @@ def setUpTestData(cls):
150151
],
151152
),
152153
)
154+
cls.egypt_tour = Tour.objects.create(guide="Amira", exhibit=cls.egypt)
155+
cls.wonders_tour = Tour.objects.create(guide="Carlos", exhibit=cls.wonders)
156+
cls.lost_tour = Tour.objects.create(guide="Yelena", exhibit=cls.lost_empires)
153157

154158
def test_filter_with_field(self):
155159
self.assertCountEqual(
@@ -296,6 +300,15 @@ def test_slice(self):
296300
MuseumExhibit.objects.filter(sections__0_1__section_number=2), [self.new_descoveries]
297301
)
298302

303+
def test_foreign_field_exact(self):
304+
qs = Tour.objects.filter(exhibit__sections__section_number=1)
305+
self.assertCountEqual(qs, [self.egypt_tour, self.wonders_tour])
306+
307+
def test_foreign_field_with_slice(self):
308+
# Only wonders exhibit has exactly two sections, and this slice matches first two
309+
qs = Tour.objects.filter(exhibit__sections__0_2__section_number__all=[1, 2])
310+
self.assertEqual(list(qs), [self.wonders_tour])
311+
299312

300313
@isolate_apps("model_fields_")
301314
class CheckTests(SimpleTestCase):

0 commit comments

Comments
 (0)