diff options
| author | Tom Christie | 2014-03-03 14:32:38 +0000 |
|---|---|---|
| committer | Tom Christie | 2014-03-03 14:32:38 +0000 |
| commit | 24a688223240eb1e71db3c0f00cd621e80cb9fb2 (patch) | |
| tree | 809035a13a8de89d020ffefea38f779bfa4a7ed3 /rest_framework/compat.py | |
| parent | ee9864e0dce10018261c131a76eb7c668703d76c (diff) | |
| parent | 3d7cb72e0a770595d8934b731f9c462b839f941a (diff) | |
| download | django-rest-framework-24a688223240eb1e71db3c0f00cd621e80cb9fb2.tar.bz2 | |
Merge pull request #1375 from linovia/feature/django_1_7
Django 1.7 compatibility
Diffstat (limited to 'rest_framework/compat.py')
| -rw-r--r-- | rest_framework/compat.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/rest_framework/compat.py b/rest_framework/compat.py index d283e2f5..3089b7fb 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -584,3 +584,23 @@ if six.PY3: else: def is_non_str_iterable(obj): return hasattr(obj, '__iter__') + + +try: + from django.utils.encoding import python_2_unicode_compatible +except ImportError: + def python_2_unicode_compatible(klass): + """ + A decorator that defines __unicode__ and __str__ methods under Python 2. + Under Python 3 it does nothing. + + To support Python 2 and 3 with a single code base, define a __str__ method + returning text and apply this decorator to the class. + """ + 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 |
