[1.7.x] Fixed #23455 -- Accept either bytes or text for related_name, convert to text.

Backport of c72eb80d11 from master.
This commit is contained in:
Carl Meyer 2014-12-11 17:28:03 -07:00
parent b376071936
commit 0a8b911582
4 changed files with 10 additions and 6 deletions

View File

@ -14,7 +14,7 @@ from django.db.models.lookups import IsNull
from django.db.models.related import RelatedObject, PathInfo from django.db.models.related import RelatedObject, PathInfo
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.db.models.sql.datastructures import Col from django.db.models.sql.datastructures import Col
from django.utils.encoding import smart_text from django.utils.encoding import force_text, smart_text
from django.utils import six from django.utils import six
from django.utils.deprecation import RenameMethodsBase, RemovedInDjango18Warning from django.utils.deprecation import RenameMethodsBase, RemovedInDjango18Warning
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -259,7 +259,7 @@ class RelatedField(Field):
sup.contribute_to_class(cls, name, virtual_only=virtual_only) sup.contribute_to_class(cls, name, virtual_only=virtual_only)
if not cls._meta.abstract and self.rel.related_name: if not cls._meta.abstract and self.rel.related_name:
related_name = self.rel.related_name % { related_name = force_text(self.rel.related_name) % {
'class': cls.__name__.lower(), 'class': cls.__name__.lower(),
'app_label': cls._meta.app_label.lower() 'app_label': cls._meta.app_label.lower()
} }

View File

@ -125,6 +125,6 @@ Bugfixes
* Fixed a crash in the admin when using "Save as new" and also deleting a * Fixed a crash in the admin when using "Save as new" and also deleting a
related inline (:ticket:`23857`). related inline (:ticket:`23857`).
* Removed auto-conversion of ``related_name`` to unicode in field * Always converted ``related_name`` to text (unicode), since that is required
deconstruction, to maintain consistency of bytes/text in migrations matching on Python 3 for interpolation. Removed conversion of ``related_name`` to text
the original values (:ticket:`23455` and :ticket:`23982`). in migration deconstruction (:ticket:`23455` and :ticket:`23982`).

View File

@ -26,7 +26,7 @@ def get_foo():
class Bar(models.Model): class Bar(models.Model):
b = models.CharField(max_length=10) b = models.CharField(max_length=10)
a = models.ForeignKey(Foo, default=get_foo) a = models.ForeignKey(Foo, default=get_foo, related_name=b'bars')
class Whiz(models.Model): class Whiz(models.Model):

View File

@ -181,6 +181,10 @@ class ForeignKeyTests(test.TestCase):
fk_model_empty = FkToChar.objects.select_related('out').get(id=fk_model_empty.pk) fk_model_empty = FkToChar.objects.select_related('out').get(id=fk_model_empty.pk)
self.assertEqual(fk_model_empty.out, char_model_empty) self.assertEqual(fk_model_empty.out, char_model_empty)
def test_related_name_converted_to_text(self):
rel_name = Bar._meta.get_field_by_name('a')[0].rel.related_name
self.assertIsInstance(rel_name, six.text_type)
class DateTimeFieldTests(unittest.TestCase): class DateTimeFieldTests(unittest.TestCase):
def test_datetimefield_to_python_usecs(self): def test_datetimefield_to_python_usecs(self):