Used constant instead of hard-coded value for recursive relationship.

This commit is contained in:
Adam Johnson 2020-01-16 08:27:04 +01:00 committed by Mariusz Felisiak
parent bf77669453
commit 1e0dcd6c8b
1 changed files with 25 additions and 9 deletions

View File

@ -606,8 +606,11 @@ class ForeignObject(RelatedField):
for index in range(len(self.from_fields)): for index in range(len(self.from_fields)):
from_field_name = self.from_fields[index] from_field_name = self.from_fields[index]
to_field_name = self.to_fields[index] to_field_name = self.to_fields[index]
from_field = (self if from_field_name == 'self' from_field = (
else self.opts.get_field(from_field_name)) self
if from_field_name == RECURSIVE_RELATIONSHIP_CONSTANT
else self.opts.get_field(from_field_name)
)
to_field = (self.remote_field.model._meta.pk if to_field_name is None to_field = (self.remote_field.model._meta.pk if to_field_name is None
else self.remote_field.model._meta.get_field(to_field_name)) else self.remote_field.model._meta.get_field(to_field_name))
related_fields.append((from_field, to_field)) related_fields.append((from_field, to_field))
@ -810,8 +813,13 @@ class ForeignKey(ForeignObject):
) )
kwargs.setdefault('db_index', True) kwargs.setdefault('db_index', True)
super().__init__(to, on_delete, from_fields=['self'], to_fields=[to_field], **kwargs) super().__init__(
to,
on_delete,
from_fields=[RECURSIVE_RELATIONSHIP_CONSTANT],
to_fields=[to_field],
**kwargs,
)
self.db_constraint = db_constraint self.db_constraint = db_constraint
def check(self, **kwargs): def check(self, **kwargs):
@ -1276,8 +1284,11 @@ class ManyToManyField(RelatedField):
"through_fields keyword argument.") % (self, from_model_name), "through_fields keyword argument.") % (self, from_model_name),
hint=( hint=(
'If you want to create a recursive relationship, ' 'If you want to create a recursive relationship, '
'use ForeignKey("self", symmetrical=False, through="%s").' 'use ForeignKey("%s", symmetrical=False, through="%s").'
) % relationship_model_name, ) % (
RECURSIVE_RELATIONSHIP_CONSTANT,
relationship_model_name,
),
obj=self, obj=self,
id='fields.E334', id='fields.E334',
) )
@ -1293,8 +1304,11 @@ class ManyToManyField(RelatedField):
"through_fields keyword argument." % (self, to_model_name), "through_fields keyword argument." % (self, to_model_name),
hint=( hint=(
'If you want to create a recursive relationship, ' 'If you want to create a recursive relationship, '
'use ForeignKey("self", symmetrical=False, through="%s").' 'use ForeignKey("%s", symmetrical=False, through="%s").'
) % relationship_model_name, ) % (
RECURSIVE_RELATIONSHIP_CONSTANT,
relationship_model_name,
),
obj=self, obj=self,
id='fields.E335', id='fields.E335',
) )
@ -1561,7 +1575,9 @@ class ManyToManyField(RelatedField):
# automatically. The funky name reduces the chance of an accidental # automatically. The funky name reduces the chance of an accidental
# clash. # clash.
if self.remote_field.symmetrical and ( if self.remote_field.symmetrical and (
self.remote_field.model == "self" or self.remote_field.model == cls._meta.object_name): self.remote_field.model == RECURSIVE_RELATIONSHIP_CONSTANT or
self.remote_field.model == cls._meta.object_name
):
self.remote_field.related_name = "%s_rel_+" % name self.remote_field.related_name = "%s_rel_+" % name
elif self.remote_field.is_hidden(): elif self.remote_field.is_hidden():
# If the backwards relation is disabled, replace the original # If the backwards relation is disabled, replace the original