Moved common code to RelatedField.deconstruct().
@@ -322,6 +322,10 @@ class RelatedField(Field):
name, path, args, kwargs = super().deconstruct()
if self.remote_field.limit_choices_to:
kwargs['limit_choices_to'] = self.remote_field.limit_choices_to
+ if self.remote_field.related_name is not None:
+ kwargs['related_name'] = self.remote_field.related_name
+ if self.remote_field.related_query_name is not None:
+ kwargs['related_query_name'] = self.remote_field.related_query_name
return name, path, args, kwargs
def get_forward_related_filter(self, obj):
@@ -561,10 +565,6 @@ class ForeignObject(RelatedField):
kwargs['from_fields'] = self.from_fields
kwargs['to_fields'] = self.to_fields
- if self.remote_field.related_name is not None:
- kwargs['related_name'] = self.remote_field.related_name
- if self.remote_field.related_query_name is not None:
- kwargs['related_query_name'] = self.remote_field.related_query_name
if self.remote_field.parent_link:
kwargs['parent_link'] = self.remote_field.parent_link
# Work out string form of "to"
@@ -1395,10 +1395,6 @@ class ManyToManyField(RelatedField):
kwargs['db_table'] = self.db_table
if self.remote_field.db_constraint is not True:
kwargs['db_constraint'] = self.remote_field.db_constraint
- if self.remote_field.related_name is not None:
- kwargs['related_name'] = self.remote_field.related_name
- if self.remote_field.related_query_name is not None:
- kwargs['related_query_name'] = self.remote_field.related_query_name
# Rel needs more work.
if isinstance(self.remote_field.model, str):
kwargs['to'] = self.remote_field.model
