Skip to content

Commit

Permalink
[2.1.x] Fixed #29653 -- Fixed missing related_query_name reverse acce…
Browse files Browse the repository at this point in the history
…ssor if GenericRelation is declared on an abstract base model.

Regression in 4ab027b.

Thanks Lauri Kainulainen for the report.

Backport of b5c7cb4 from master
  • Loading branch information
ramiro authored and timgraham committed Aug 10, 2018
1 parent d761567 commit f72a7d8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ def __new__(cls, name, bases, attrs, **kwargs):
)
else:
field = copy.deepcopy(field)
field.mti_inherited = True
if not base._meta.abstract:
field.mti_inherited = True
new_class.add_to_class(field.name, field)

# Copy indexes so that index names are unique when models extend an
Expand Down
4 changes: 4 additions & 0 deletions docs/releases/2.1.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ Bugfixes
* Fixed a regression where the admin change form crashed if the user doesn't
have the 'add' permission to a model that uses ``TabularInline``
(:ticket:`29637`).

* Fixed a regression where a ``related_query_name`` reverse accessor wasn't set
up when a ``GenericRelation`` is declared on an abstract base model
(:ticket:`29653`).
2 changes: 1 addition & 1 deletion tests/generic_relations_regress/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def save_form_data(self, *args, **kwargs):


class HasLinks(models.Model):
links = SpecialGenericRelation(Link)
links = SpecialGenericRelation(Link, related_query_name='targets')

class Meta:
abstract = True
Expand Down
9 changes: 9 additions & 0 deletions tests/generic_relations_regress/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,12 @@ def test_generic_reverse_relation_with_mti(self):
link = Link.objects.create(content_object=place)
result = Link.objects.filter(places=place)
self.assertCountEqual(result, [link])

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])

0 comments on commit f72a7d8

Please sign in to comment.