Code

Ticket #7666: 7666-failing-test.diff

File 7666-failing-test.diff, 1.5 KB (added by jkocherhans, 6 years ago)
Line 
1Index: tests/regressiontests/reverse_single_related/__init__.py
2===================================================================
3Index: tests/regressiontests/reverse_single_related/models.py
4===================================================================
5--- tests/regressiontests/reverse_single_related/models.py      (revision 0)
6+++ tests/regressiontests/reverse_single_related/models.py      (revision 0)
7@@ -0,0 +1,43 @@
8+"""
9+Regression tests for an object that cannot access a single related object due
10+to a restrictive default manager.
11+"""
12+
13+from django.db import models
14+
15+
16+class SourceManager(models.Manager):
17+    def get_query_set(self):
18+        return super(SourceManager, self).get_query_set().filter(is_public=True)
19+
20+class Source(models.Model):
21+    is_public = models.BooleanField()
22+    objects = SourceManager()
23+
24+class Item(models.Model):
25+    source = models.ForeignKey(Source)
26+
27+
28+__test__ = {'API_TESTS':"""
29+
30+>>> public_source = Source.objects.create(is_public=True)
31+>>> public_item = Item.objects.create(source=public_source)
32+
33+>>> private_source = Source.objects.create(is_public=False)
34+>>> private_item = Item.objects.create(source=private_source)
35+
36+Only one source is available via all() due to the custom default manager.
37+
38+>>> Source.objects.all()
39+[<Source: Source object>]
40+
41+>>> public_item.source
42+<Source: Source object>
43+
44+Make sure that an item can still access its related source even if the default
45+manager doesn't normally allow it.
46+
47+>>> private_item.source
48+<Source: Source object>
49+
50+"""}