mirror of
https://github.com/django/django.git
synced 2025-10-26 15:16:09 +00:00
Fixed #21198 -- Prevented invalid use of @python_2_unicode_compatible.
Thanks jpic for the report and chmodas for working on a patch.
Reverts 2ea80b94. Refs #19362.
This commit is contained in:
@@ -450,11 +450,6 @@ class Model(six.with_metaclass(ModelBase)):
|
||||
|
||||
def __str__(self):
|
||||
if six.PY2 and hasattr(self, '__unicode__'):
|
||||
if type(self).__unicode__ == Model.__str__:
|
||||
klass_name = type(self).__name__
|
||||
raise RuntimeError("%s.__unicode__ is aliased to __str__. Did"
|
||||
" you apply @python_2_unicode_compatible"
|
||||
" without defining __str__?" % klass_name)
|
||||
return force_text(self).encode('utf-8')
|
||||
return '%s object' % self.__class__.__name__
|
||||
|
||||
|
||||
@@ -28,6 +28,10 @@ def python_2_unicode_compatible(klass):
|
||||
returning text and apply this decorator to the class.
|
||||
"""
|
||||
if six.PY2:
|
||||
if '__str__' not in klass.__dict__:
|
||||
raise ValueError("@python_2_unicode_compatible cannot be applied "
|
||||
"to %s because it doesn't define __str__()." %
|
||||
klass.__name__)
|
||||
klass.__unicode__ = klass.__str__
|
||||
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
|
||||
return klass
|
||||
|
||||
Reference in New Issue
Block a user