diff options
| author | Gabe Jackson <gabejackson@cxg.ch> | 2014-03-04 12:23:32 +0100 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2014-03-05 22:37:53 +0200 |
| commit | b77f26313cddbfde20dcf2661e9bd35458c2d1bd (patch) | |
| tree | ed655a90373cf606e304e82eaeceaf025c892328 /tests/generic_relations | |
| parent | c627da0ccc12861163f28177aa7538b420a9d310 (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')
| -rw-r--r-- | tests/generic_relations/models.py | 4 | ||||
| -rw-r--r-- | tests/generic_relations/tests.py | 18 |
2 files changed, 20 insertions, 2 deletions
diff --git a/tests/generic_relations/models.py b/tests/generic_relations/models.py index 436f26afb6..5d24e017a2 100644 --- a/tests/generic_relations/models.py +++ b/tests/generic_relations/models.py @@ -68,7 +68,7 @@ class Animal(models.Model): common_name = models.CharField(max_length=150) latin_name = models.CharField(max_length=150) - tags = GenericRelation(TaggedItem) + tags = GenericRelation(TaggedItem, related_query_name='animal') comparisons = GenericRelation(Comparison, object_id_field="object_id1", content_type_field="content_type1") @@ -116,7 +116,7 @@ class Rock(Mineral): class ManualPK(models.Model): id = models.IntegerField(primary_key=True) - tags = GenericRelation(TaggedItem) + tags = GenericRelation(TaggedItem, related_query_name='manualpk') class ForProxyModelModel(models.Model): 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. |
