Skip to content

Commit c72eb80

Browse files
committed
Fixed #23455 -- Accept either bytes or text for related_name, convert to text.
1 parent d4bddde commit c72eb80

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

django/db/models/fields/related.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from django.db.models.related import RelatedObject, PathInfo
1515
from django.db.models.query import QuerySet
1616
from django.db.models.expressions import Col
17-
from django.utils.encoding import smart_text
17+
from django.utils.encoding import force_text, smart_text
1818
from django.utils import six
1919
from django.utils.translation import ugettext_lazy as _
2020
from django.utils.functional import curry, cached_property
@@ -280,7 +280,7 @@ def contribute_to_class(self, cls, name, virtual_only=False):
280280
sup.contribute_to_class(cls, name, virtual_only=virtual_only)
281281

282282
if not cls._meta.abstract and self.rel.related_name:
283-
related_name = self.rel.related_name % {
283+
related_name = force_text(self.rel.related_name) % {
284284
'class': cls.__name__.lower(),
285285
'app_label': cls._meta.app_label.lower()
286286
}

docs/releases/1.7.2.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ Bugfixes
125125
* Fixed a crash in the admin when using "Save as new" and also deleting a
126126
related inline (:ticket:`23857`).
127127

128-
* Removed auto-conversion of ``related_name`` to unicode in field
129-
deconstruction, to maintain consistency of bytes/text in migrations matching
130-
the original values (:ticket:`23455` and :ticket:`23982`).
128+
* Always converted ``related_name`` to text (unicode), since that is required
129+
on Python 3 for interpolation. Removed conversion of ``related_name`` to text
130+
in migration deconstruction (:ticket:`23455` and :ticket:`23982`).

tests/model_fields/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_foo():
2525

2626
class Bar(models.Model):
2727
b = models.CharField(max_length=10)
28-
a = models.ForeignKey(Foo, default=get_foo)
28+
a = models.ForeignKey(Foo, default=get_foo, related_name=b'bars')
2929

3030

3131
class Whiz(models.Model):

tests/model_fields/tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ class FKUniqueTrue(models.Model):
199199
warnings = checks.run_checks()
200200
self.assertEqual(warnings, expected_warnings)
201201

202+
def test_related_name_converted_to_text(self):
203+
rel_name = Bar._meta.get_field_by_name('a')[0].rel.related_name
204+
self.assertIsInstance(rel_name, six.text_type)
205+
202206

203207
class DateTimeFieldTests(unittest.TestCase):
204208
def test_datetimefield_to_python_usecs(self):

0 commit comments

Comments
 (0)