summaryrefslogtreecommitdiff
path: root/tests/generic_relations/tests.py
diff options
context:
space:
mode:
authorGabe Jackson <gabejackson@cxg.ch>2014-03-04 12:23:32 +0100
committerAnssi Kääriäinen <akaariai@gmail.com>2014-03-05 22:37:53 +0200
commitb77f26313cddbfde20dcf2661e9bd35458c2d1bd (patch)
treeed655a90373cf606e304e82eaeceaf025c892328 /tests/generic_relations/tests.py
parentc627da0ccc12861163f28177aa7538b420a9d310 (diff)
Fixed #22207 -- Added support for GenericRelation reverse lookups
GenericRelation now supports an optional related_query_name argument. Setting related_query_name adds a relation from the related object back to the content type for filtering, ordering and other query operations. Thanks to Loic Bistuer for spotting a couple of important issues in his review.
Diffstat (limited to 'tests/generic_relations/tests.py')
-rw-r--r--tests/generic_relations/tests.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/tests/generic_relations/tests.py b/tests/generic_relations/tests.py
index ed12c5e92b..ae90ace34a 100644
--- a/tests/generic_relations/tests.py
+++ b/tests/generic_relations/tests.py
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
from django import forms
from django.contrib.contenttypes.forms import generic_inlineformset_factory
from django.contrib.contenttypes.models import ContentType
+from django.core.exceptions import FieldError
from django.test import TestCase
from django.utils import six
@@ -42,6 +43,17 @@ class GenericRelationsTests(TestCase):
# You can easily access the content object like a foreign key.
t = TaggedItem.objects.get(tag="salty")
self.assertEqual(t.content_object, bacon)
+ qs = TaggedItem.objects.filter(animal__isnull=False).order_by('animal__common_name', 'tag')
+ self.assertQuerysetEqual(
+ qs, ["<TaggedItem: hairy>", "<TaggedItem: yellow>", "<TaggedItem: fatty>"]
+ )
+ mpk = ManualPK.objects.create(id=1)
+ mpk.tags.create(tag='mpk')
+ from django.db.models import Q
+ qs = TaggedItem.objects.filter(Q(animal__isnull=False) | Q(manualpk__id=1)).order_by('tag')
+ self.assertQuerysetEqual(
+ qs, ["fatty", "hairy", "mpk", "yellow"], lambda x: x.tag)
+ mpk.delete()
# Recall that the Mineral class doesn't have an explicit GenericRelation
# defined. That's OK, because you can create TaggedItems explicitly.
@@ -151,6 +163,12 @@ class GenericRelationsTests(TestCase):
"<Animal: Platypus>"
])
+ def test_generic_relation_related_name_default(self):
+ # Test that GenericRelation by default isn't usable from
+ # the reverse side.
+ with self.assertRaises(FieldError):
+ TaggedItem.objects.filter(vegetable__isnull=True)
+
def test_multiple_gfk(self):
# Simple tests for multiple GenericForeignKeys
# only uses one model, since the above tests should be sufficient.