Skip to content

Fixed #22207 -- Added `related_name` to GenericRelation to generate reverse relation on related object #2393

Closed
wants to merge 4 commits into from

4 participants

@gabejackson

GenericRelation now supports an optional related_name argument. Setting
related_name adds a relation from the related object back to the content
type for filtering, ordering and other query operations.

gabejackson added some commits Mar 4, 2014
@gabejackson gabejackson Added support for GenericRelation reverse lookups
GenericRelation now supports an optional related_name argument. Setting
related_name adds a relation from the related object back to the content
type for filtering, ordering and other query operations.
6b59bb1
@gabejackson gabejackson Fixed failing tests for #22207 33f84b6
@akaariai akaariai commented on an outdated diff Mar 5, 2014
django/contrib/admin/utils.py
@@ -168,7 +168,7 @@ def add_edge(self, source, target):
def collect(self, objs, source=None, source_attr=None, **kwargs):
for obj in objs:
- if source_attr:
+ if source_attr and source_attr != '+':
@akaariai
Django member
akaariai added a note Mar 5, 2014

This should be actually .endswith('+'). (gabejackson: no need to fix this, I'll do that when committing).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@loic loic and 1 other commented on an outdated diff Mar 5, 2014
tests/generic_relations/tests.py
@@ -42,6 +43,18 @@ def test_generic_relations(self):
# 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()
+
@loic
Django member
loic added a note Mar 5, 2014

pep8: extra blank line.

@gabejackson
gabejackson added a note Mar 5, 2014

thx, sorry. thought i checked it with flake8... hm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@loic loic commented on an outdated diff Mar 5, 2014
django/contrib/contenttypes/fields.py
@@ -30,11 +30,13 @@ class GenericForeignKey(six.with_metaclass(RenameGenericForeignKeyMethods)):
fields.
"""
- def __init__(self, ct_field="content_type", fk_field="object_id", for_concrete_model=True):
+ def __init__(self, ct_field="content_type", fk_field="object_id", for_concrete_model=True,
@loic
Django member
loic added a note Mar 5, 2014

Why GenericForeignKey (i.e. not GenericRelation) gets a related_name?

Btw do you see my mentions on IRC? It's like everyone disappeared at once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
gabejackson added some commits Mar 5, 2014
@gabejackson gabejackson Changed related_name to related_query_name on GenericRelation
Removed related_name from GFK and changed related_name to
related_query_name on GenericRelation. Updates Docs.
594d17b
@gabejackson gabejackson Fixed prefetch_related tests 1c08386
@ramiro
Django member
ramiro commented May 12, 2014

Merged in b77f263.

Thanks!

@ramiro ramiro closed this May 12, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.