1
0
mirror of https://github.com/django/django.git synced 2025-10-26 15:16:09 +00:00

Fixed #25858 -- Bound abstract model application relative relationships.

Thanks to Karl Hobley for the report and Markus, Shai, Aymeric for their input
and Tim for the review.
This commit is contained in:
Simon Charette
2016-01-09 18:05:57 -05:00
parent 2ed2db2ea3
commit bc7d201bdb
3 changed files with 63 additions and 6 deletions

View File

@@ -34,7 +34,7 @@ from .reverse_related import (
RECURSIVE_RELATIONSHIP_CONSTANT = 'self'
def resolve_relation(scope_model, relation):
def resolve_relation(scope_model, relation, resolve_recursive_relationship=True):
"""
Transform relation into a model or fully-qualified model string of the form
"app_label.ModelName", relative to scope_model.
@@ -49,12 +49,11 @@ def resolve_relation(scope_model, relation):
"""
# Check for recursive relations
if relation == RECURSIVE_RELATIONSHIP_CONSTANT:
relation = scope_model
if resolve_recursive_relationship:
relation = scope_model
# Look for an "app.Model" relation
if isinstance(relation, six.string_types):
if "." not in relation:
relation = "%s.%s" % (scope_model._meta.app_label, relation)
elif isinstance(relation, six.string_types) and '.' not in relation:
relation = "%s.%s" % (scope_model._meta.app_label, relation)
return relation
@@ -301,6 +300,11 @@ class RelatedField(Field):
field.remote_field.model = related
field.do_related_class(related, model)
lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
else:
# Bind a lazy reference to the app in which the model is defined.
self.remote_field.model = resolve_relation(
cls, self.remote_field.model, resolve_recursive_relationship=False
)
def get_forward_related_filter(self, obj):
"""
@@ -1553,6 +1557,11 @@ class ManyToManyField(RelatedField):
lazy_related_operation(resolve_through_model, cls, self.remote_field.through, field=self)
elif not cls._meta.swapped:
self.remote_field.through = create_many_to_many_intermediary_model(self, cls)
else:
# Bind a lazy reference to the app in which the model is defined.
self.remote_field.through = resolve_relation(
cls, self.remote_field.through, resolve_recursive_relationship=False
)
# Add the descriptor for the m2m relation.
setattr(cls, self.name, ManyToManyDescriptor(self.remote_field, reverse=False))