diff options
| author | robwa <robwa@users.noreply.github.com> | 2018-03-13 03:42:48 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-03-12 22:42:48 -0400 |
| commit | 4ab027b94409e6415b774797bf9d3593da9d9ea8 (patch) | |
| tree | 03c9c8d5c2906658877cbb848c013a9bb1036cc7 /django | |
| parent | b639bcc2503d66870f72bdffc01abc015a548462 (diff) | |
Fixed #28988 -- Fixed queries when a GenericRelation is used with multi-table inheritance.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/contenttypes/fields.py | 8 | ||||
| -rw-r--r-- | django/db/models/base.py | 4 |
2 files changed, 11 insertions, 1 deletions
diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py index d186e1f39a..e55766aa6f 100644 --- a/django/contrib/contenttypes/fields.py +++ b/django/contrib/contenttypes/fields.py @@ -283,6 +283,8 @@ class GenericRelation(ForeignObject): rel_class = GenericRel + mti_inherited = False + def __init__(self, to, object_id_field='object_id', content_type_field='content_type', for_concrete_model=True, related_query_name=None, limit_choices_to=None, **kwargs): kwargs['rel'] = self.rel_class( @@ -427,6 +429,12 @@ class GenericRelation(ForeignObject): kwargs['private_only'] = True super().contribute_to_class(cls, name, **kwargs) self.model = cls + # Disable the reverse relation for fields inherited by subclasses of a + # model in multi-table inheritance. The reverse relation points to the + # field of the base model. + if self.mti_inherited: + self.remote_field.related_name = '+' + self.remote_field.related_query_name = None setattr(cls, self.name, ReverseGenericManyToOneDescriptor(self.remote_field)) # Add get_RELATED_order() and set_RELATED_order() to the model this diff --git a/django/db/models/base.py b/django/db/models/base.py index 20d1c73ddf..9af6a8f92d 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -278,7 +278,9 @@ class ModelBase(type): ) ) else: - new_class.add_to_class(field.name, copy.deepcopy(field)) + field = copy.deepcopy(field) + field.mti_inherited = True + new_class.add_to_class(field.name, field) # Copy indexes so that index names are unique when models extend an # abstract model. |
