2019-08-20 15:54:41 +08:00
|
|
|
from django.db.models import ProtectedError, Q, Sum
|
2013-11-13 03:35:52 +08:00
|
|
|
from django.forms.models import modelform_factory
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.test import TestCase, skipIfDBFeature
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2013-06-06 02:16:16 +08:00
|
|
|
from .models import (
|
2017-12-06 05:08:50 +08:00
|
|
|
A,
|
|
|
|
Address,
|
|
|
|
B,
|
|
|
|
Board,
|
|
|
|
C,
|
|
|
|
Cafe,
|
|
|
|
CharLink,
|
|
|
|
Company,
|
|
|
|
Contact,
|
|
|
|
Content,
|
|
|
|
D,
|
|
|
|
Guild,
|
|
|
|
HasLinkThing,
|
|
|
|
Link,
|
|
|
|
Node,
|
|
|
|
Note,
|
|
|
|
OddRelation1,
|
|
|
|
OddRelation2,
|
|
|
|
Organization,
|
|
|
|
Person,
|
|
|
|
Place,
|
|
|
|
Related,
|
|
|
|
Restaurant,
|
|
|
|
Tag,
|
|
|
|
Team,
|
|
|
|
TextLink,
|
2014-07-15 00:42:14 +08:00
|
|
|
)
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2009-04-04 03:52:14 +08:00
|
|
|
|
|
|
|
class GenericRelationTests(TestCase):
|
|
|
|
def test_inherited_models_content_type(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
GenericRelations on inherited classes use the correct content type.
|
2009-04-04 03:52:14 +08:00
|
|
|
"""
|
|
|
|
p = Place.objects.create(name="South Park")
|
2010-01-23 02:39:48 +08:00
|
|
|
r = Restaurant.objects.create(name="Chubby's")
|
2009-04-04 03:52:14 +08:00
|
|
|
l1 = Link.objects.create(content_object=p)
|
|
|
|
l2 = Link.objects.create(content_object=r)
|
|
|
|
self.assertEqual(list(p.links.all()), [l1])
|
|
|
|
self.assertEqual(list(r.links.all()), [l2])
|
2010-01-23 02:39:48 +08:00
|
|
|
|
|
|
|
def test_reverse_relation_pk(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The correct column name is used for the primary key on the
|
2010-01-23 02:39:48 +08:00
|
|
|
originating model of a query. See #12664.
|
|
|
|
"""
|
|
|
|
p = Person.objects.create(account=23, name="Chef")
|
2013-06-06 02:16:16 +08:00
|
|
|
Address.objects.create(
|
|
|
|
street="123 Anywhere Place",
|
|
|
|
city="Conifer",
|
|
|
|
state="CO",
|
|
|
|
zipcode="80433",
|
|
|
|
content_object=p,
|
|
|
|
)
|
2010-01-23 02:39:48 +08:00
|
|
|
|
|
|
|
qs = Person.objects.filter(addresses__zipcode="80433")
|
|
|
|
self.assertEqual(1, qs.count())
|
|
|
|
self.assertEqual("Chef", qs[0].name)
|
2010-01-30 01:58:22 +08:00
|
|
|
|
|
|
|
def test_charlink_delete(self):
|
|
|
|
oddrel = OddRelation1.objects.create(name="clink")
|
2013-06-06 02:16:16 +08:00
|
|
|
CharLink.objects.create(content_object=oddrel)
|
2010-01-30 01:58:22 +08:00
|
|
|
oddrel.delete()
|
|
|
|
|
|
|
|
def test_textlink_delete(self):
|
|
|
|
oddrel = OddRelation2.objects.create(name="tlink")
|
2013-06-06 02:16:16 +08:00
|
|
|
TextLink.objects.create(content_object=oddrel)
|
2010-01-30 01:58:22 +08:00
|
|
|
oddrel.delete()
|
2010-02-11 03:21:52 +08:00
|
|
|
|
2017-11-29 14:06:45 +08:00
|
|
|
def test_coerce_object_id_remote_field_cache_persistence(self):
|
|
|
|
restaurant = Restaurant.objects.create()
|
|
|
|
CharLink.objects.create(content_object=restaurant)
|
|
|
|
charlink = CharLink.objects.latest("pk")
|
|
|
|
self.assertIs(charlink.content_object, charlink.content_object)
|
2017-12-06 05:08:50 +08:00
|
|
|
# If the model (Cafe) uses more than one level of multi-table inheritance.
|
|
|
|
cafe = Cafe.objects.create()
|
|
|
|
CharLink.objects.create(content_object=cafe)
|
|
|
|
charlink = CharLink.objects.latest("pk")
|
|
|
|
self.assertIs(charlink.content_object, charlink.content_object)
|
2017-11-29 14:06:45 +08:00
|
|
|
|
2010-02-11 03:21:52 +08:00
|
|
|
def test_q_object_or(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
SQL query parameters for generic relations are properly
|
|
|
|
grouped when OR is used (#11535).
|
2010-02-11 03:21:52 +08:00
|
|
|
|
|
|
|
In this bug the first query (below) works while the second, with the
|
|
|
|
query parameters the same but in reverse order, does not.
|
|
|
|
|
|
|
|
The issue is that the generic relation conditions do not get properly
|
|
|
|
grouped in parentheses.
|
|
|
|
"""
|
|
|
|
note_contact = Contact.objects.create()
|
|
|
|
org_contact = Contact.objects.create()
|
2013-06-06 02:16:16 +08:00
|
|
|
Note.objects.create(note="note", content_object=note_contact)
|
2010-02-11 03:21:52 +08:00
|
|
|
org = Organization.objects.create(name="org name")
|
|
|
|
org.contacts.add(org_contact)
|
|
|
|
# search with a non-matching note and a matching org name
|
|
|
|
qs = Contact.objects.filter(
|
|
|
|
Q(notes__note__icontains=r"other note")
|
2013-06-06 02:16:16 +08:00
|
|
|
| Q(organizations__name__icontains=r"org name")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(org_contact, qs)
|
2010-02-11 03:21:52 +08:00
|
|
|
# search again, with the same query parameters, in reverse order
|
|
|
|
qs = Contact.objects.filter(
|
|
|
|
Q(organizations__name__icontains=r"org name")
|
|
|
|
| Q(notes__note__icontains=r"other note")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(org_contact, qs)
|
2010-02-11 03:21:52 +08:00
|
|
|
|
2012-12-21 03:25:48 +08:00
|
|
|
def test_join_reuse(self):
|
|
|
|
qs = Person.objects.filter(addresses__street="foo").filter(
|
|
|
|
addresses__street="bar"
|
|
|
|
)
|
|
|
|
self.assertEqual(str(qs.query).count("JOIN"), 2)
|
2010-02-11 03:21:52 +08:00
|
|
|
|
2013-02-02 09:10:16 +08:00
|
|
|
def test_generic_relation_ordering(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Ordering over a generic relation does not include extraneous
|
2013-02-02 09:10:16 +08:00
|
|
|
duplicate results, nor excludes rows not participating in the relation.
|
|
|
|
"""
|
|
|
|
p1 = Place.objects.create(name="South Park")
|
|
|
|
p2 = Place.objects.create(name="The City")
|
|
|
|
c = Company.objects.create(name="Chubby's Intl.")
|
2013-06-06 02:16:16 +08:00
|
|
|
Link.objects.create(content_object=p1)
|
|
|
|
Link.objects.create(content_object=c)
|
2013-02-02 09:10:16 +08:00
|
|
|
|
|
|
|
places = list(Place.objects.order_by("links__id"))
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-02-02 09:10:16 +08:00
|
|
|
def count_places(place):
|
2013-02-02 17:42:01 +08:00
|
|
|
return len([p for p in places if p.id == place.id])
|
2013-02-02 09:10:16 +08:00
|
|
|
|
|
|
|
self.assertEqual(len(places), 2)
|
|
|
|
self.assertEqual(count_places(p1), 1)
|
|
|
|
self.assertEqual(count_places(p2), 1)
|
2013-02-06 10:35:10 +08:00
|
|
|
|
|
|
|
def test_target_model_len_zero(self):
|
2016-06-28 23:21:26 +08:00
|
|
|
"""
|
|
|
|
Saving a model with a GenericForeignKey to a model instance whose
|
|
|
|
__len__ method returns 0 (Team.__len__() here) shouldn't fail (#13085).
|
|
|
|
"""
|
2013-02-06 10:35:10 +08:00
|
|
|
team1 = Team.objects.create(name="Backend devs")
|
2016-06-28 23:21:26 +08:00
|
|
|
note = Note(note="Deserve a bonus", content_object=team1)
|
2013-02-06 10:35:10 +08:00
|
|
|
note.save()
|
|
|
|
|
2017-01-19 22:55:03 +08:00
|
|
|
def test_target_model_bool_false(self):
|
|
|
|
"""
|
|
|
|
Saving a model with a GenericForeignKey to a model instance whose
|
|
|
|
__bool__ method returns False (Guild.__bool__() here) shouldn't fail
|
|
|
|
(#13085).
|
|
|
|
"""
|
2013-02-06 10:35:10 +08:00
|
|
|
g1 = Guild.objects.create(name="First guild")
|
|
|
|
note = Note(note="Note for guild", content_object=g1)
|
|
|
|
note.save()
|
|
|
|
|
2013-02-23 02:59:11 +08:00
|
|
|
@skipIfDBFeature("interprets_empty_strings_as_nulls")
|
2013-02-06 10:35:10 +08:00
|
|
|
def test_gfk_to_model_with_empty_pk(self):
|
|
|
|
"""Test related to #13085"""
|
|
|
|
# Saving model with GenericForeignKey to model instance with an
|
|
|
|
# empty CharField PK
|
|
|
|
b1 = Board.objects.create(name="")
|
|
|
|
tag = Tag(label="VP", content_object=b1)
|
|
|
|
tag.save()
|
2013-05-20 22:40:34 +08:00
|
|
|
|
|
|
|
def test_ticket_20378(self):
|
2015-02-02 19:48:30 +08:00
|
|
|
# Create a couple of extra HasLinkThing so that the autopk value
|
|
|
|
# isn't the same for Link and HasLinkThing.
|
2013-05-20 22:40:34 +08:00
|
|
|
hs1 = HasLinkThing.objects.create()
|
|
|
|
hs2 = HasLinkThing.objects.create()
|
2015-02-02 19:48:30 +08:00
|
|
|
hs3 = HasLinkThing.objects.create()
|
|
|
|
hs4 = HasLinkThing.objects.create()
|
|
|
|
l1 = Link.objects.create(content_object=hs3)
|
|
|
|
l2 = Link.objects.create(content_object=hs4)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(HasLinkThing.objects.filter(links=l1), [hs3])
|
|
|
|
self.assertSequenceEqual(HasLinkThing.objects.filter(links=l2), [hs4])
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HasLinkThing.objects.exclude(links=l2), [hs1, hs2, hs3]
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
HasLinkThing.objects.exclude(links=l1), [hs1, hs2, hs4]
|
|
|
|
)
|
2013-06-06 05:29:44 +08:00
|
|
|
|
|
|
|
def test_ticket_20564(self):
|
|
|
|
b1 = B.objects.create()
|
|
|
|
b2 = B.objects.create()
|
|
|
|
b3 = B.objects.create()
|
|
|
|
c1 = C.objects.create(b=b1)
|
|
|
|
c2 = C.objects.create(b=b2)
|
|
|
|
c3 = C.objects.create(b=b3)
|
|
|
|
A.objects.create(flag=None, content_object=b1)
|
|
|
|
A.objects.create(flag=True, content_object=b2)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(C.objects.filter(b__a__flag=None), [c1, c3])
|
|
|
|
self.assertSequenceEqual(C.objects.exclude(b__a__flag=None), [c2])
|
2013-06-06 05:29:44 +08:00
|
|
|
|
|
|
|
def test_ticket_20564_nullable_fk(self):
|
|
|
|
b1 = B.objects.create()
|
|
|
|
b2 = B.objects.create()
|
|
|
|
b3 = B.objects.create()
|
|
|
|
d1 = D.objects.create(b=b1)
|
|
|
|
d2 = D.objects.create(b=b2)
|
|
|
|
d3 = D.objects.create(b=b3)
|
|
|
|
d4 = D.objects.create()
|
|
|
|
A.objects.create(flag=None, content_object=b1)
|
|
|
|
A.objects.create(flag=True, content_object=b1)
|
|
|
|
A.objects.create(flag=True, content_object=b2)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(D.objects.exclude(b__a__flag=None), [d2])
|
|
|
|
self.assertSequenceEqual(D.objects.filter(b__a__flag=None), [d1, d3, d4])
|
|
|
|
self.assertSequenceEqual(B.objects.filter(a__flag=None), [b1, b3])
|
|
|
|
self.assertSequenceEqual(B.objects.exclude(a__flag=None), [b2])
|
2013-06-06 05:29:44 +08:00
|
|
|
|
|
|
|
def test_extra_join_condition(self):
|
|
|
|
# A crude check that content_type_id is taken in account in the
|
|
|
|
# join/subquery condition.
|
|
|
|
self.assertIn(
|
|
|
|
"content_type_id", str(B.objects.exclude(a__flag=None).query).lower()
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2013-06-06 05:29:44 +08:00
|
|
|
# No need for any joins - the join from inner query can be trimmed in
|
|
|
|
# this case (but not in the above case as no a objects at all for given
|
|
|
|
# B would then fail).
|
|
|
|
self.assertNotIn(" join ", str(B.objects.exclude(a__flag=True).query).lower())
|
|
|
|
self.assertIn(
|
|
|
|
"content_type_id", str(B.objects.exclude(a__flag=True).query).lower()
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2013-11-06 02:02:24 +08:00
|
|
|
|
|
|
|
def test_annotate(self):
|
|
|
|
hs1 = HasLinkThing.objects.create()
|
2014-12-17 15:47:58 +08:00
|
|
|
hs2 = HasLinkThing.objects.create()
|
|
|
|
HasLinkThing.objects.create()
|
2013-11-06 02:02:24 +08:00
|
|
|
b = Board.objects.create(name=str(hs1.pk))
|
2014-12-17 15:47:58 +08:00
|
|
|
Link.objects.create(content_object=hs2)
|
2016-11-15 06:40:28 +08:00
|
|
|
link = Link.objects.create(content_object=hs1)
|
2013-11-06 02:02:24 +08:00
|
|
|
Link.objects.create(content_object=b)
|
2014-12-17 15:47:58 +08:00
|
|
|
qs = HasLinkThing.objects.annotate(Sum("links")).filter(pk=hs1.pk)
|
2013-11-06 02:02:24 +08:00
|
|
|
# If content_type restriction isn't in the query's join condition,
|
2013-11-25 22:12:43 +08:00
|
|
|
# then wrong results are produced here as the link to b will also match
|
|
|
|
# (b and hs1 have equal pks).
|
2013-11-06 02:02:24 +08:00
|
|
|
self.assertEqual(qs.count(), 1)
|
2016-11-15 06:40:28 +08:00
|
|
|
self.assertEqual(qs[0].links__sum, link.id)
|
|
|
|
link.delete()
|
2013-11-06 02:02:24 +08:00
|
|
|
# Now if we don't have proper left join, we will not produce any
|
|
|
|
# results at all here.
|
|
|
|
# clear cached results
|
|
|
|
qs = qs.all()
|
|
|
|
self.assertEqual(qs.count(), 1)
|
|
|
|
# Note - 0 here would be a nicer result...
|
|
|
|
self.assertIs(qs[0].links__sum, None)
|
|
|
|
# Finally test that filtering works.
|
|
|
|
self.assertEqual(qs.filter(links__sum__isnull=True).count(), 1)
|
|
|
|
self.assertEqual(qs.filter(links__sum__isnull=False).count(), 0)
|
2013-11-13 03:35:52 +08:00
|
|
|
|
2014-12-17 15:47:58 +08:00
|
|
|
def test_filter_targets_related_pk(self):
|
2021-07-08 19:13:40 +08:00
|
|
|
# Use hardcoded PKs to ensure different PKs for "link" and "hs2"
|
|
|
|
# objects.
|
|
|
|
HasLinkThing.objects.create(pk=1)
|
|
|
|
hs2 = HasLinkThing.objects.create(pk=2)
|
|
|
|
link = Link.objects.create(content_object=hs2, pk=1)
|
2016-11-15 06:40:28 +08:00
|
|
|
self.assertNotEqual(link.object_id, link.pk)
|
|
|
|
self.assertSequenceEqual(HasLinkThing.objects.filter(links=link.pk), [hs2])
|
2014-12-17 15:47:58 +08:00
|
|
|
|
2013-11-13 03:35:52 +08:00
|
|
|
def test_editable_generic_rel(self):
|
|
|
|
GenericRelationForm = modelform_factory(HasLinkThing, fields="__all__")
|
|
|
|
form = GenericRelationForm()
|
|
|
|
self.assertIn("links", form.fields)
|
|
|
|
form = GenericRelationForm({"links": None})
|
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
form.save()
|
2015-01-07 08:16:35 +08:00
|
|
|
links = HasLinkThing._meta.get_field("links")
|
2013-11-13 03:35:52 +08:00
|
|
|
self.assertEqual(links.save_form_data_calls, 1)
|
2014-07-15 00:42:14 +08:00
|
|
|
|
|
|
|
def test_ticket_22998(self):
|
|
|
|
related = Related.objects.create()
|
|
|
|
content = Content.objects.create(related_obj=related)
|
2014-07-16 17:48:49 +08:00
|
|
|
Node.objects.create(content=content)
|
2014-07-15 00:42:14 +08:00
|
|
|
|
|
|
|
# deleting the Related cascades to the Content cascades to the Node,
|
|
|
|
# where the pre_delete signal should fire and prevent deletion.
|
|
|
|
with self.assertRaises(ProtectedError):
|
|
|
|
related.delete()
|
2014-08-07 14:06:44 +08:00
|
|
|
|
|
|
|
def test_ticket_22982(self):
|
|
|
|
place = Place.objects.create(name="My Place")
|
|
|
|
self.assertIn("GenericRelatedObjectManager", str(place.links))
|
2017-07-22 06:21:13 +08:00
|
|
|
|
|
|
|
def test_filter_on_related_proxy_model(self):
|
|
|
|
place = Place.objects.create()
|
|
|
|
Link.objects.create(content_object=place)
|
|
|
|
self.assertEqual(Place.objects.get(link_proxy__object_id=place.id), place)
|
2018-03-13 10:42:48 +08:00
|
|
|
|
|
|
|
def test_generic_reverse_relation_with_mti(self):
|
|
|
|
"""
|
|
|
|
Filtering with a reverse generic relation, where the GenericRelation
|
|
|
|
comes from multi-table inheritance.
|
|
|
|
"""
|
|
|
|
place = Place.objects.create(name="Test Place")
|
|
|
|
link = Link.objects.create(content_object=place)
|
|
|
|
result = Link.objects.filter(places=place)
|
|
|
|
self.assertCountEqual(result, [link])
|
2018-08-09 09:25:18 +08:00
|
|
|
|
|
|
|
def test_generic_reverse_relation_with_abc(self):
|
|
|
|
"""
|
|
|
|
The reverse generic relation accessor (targets) is created if the
|
|
|
|
GenericRelation comes from an abstract base model (HasLinks).
|
|
|
|
"""
|
|
|
|
thing = HasLinkThing.objects.create()
|
|
|
|
link = Link.objects.create(content_object=thing)
|
|
|
|
self.assertCountEqual(link.targets.all(), [thing])
|