Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use python_2_unicode_compatible backported from django 1.5

  • Loading branch information...
commit 78fdfc0f64bec323eb06a126d28021f70a156cac 1 parent c673658
@graingert authored
Showing with 18 additions and 2 deletions.
  1. +18 −2 templatetag_sugar/tests/models.py
View
20 templatetag_sugar/tests/models.py
@@ -1,8 +1,24 @@
from django.db import models
+import six
+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 not six.PY3:
+ klass.__unicode__ = klass.__str__
+ klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
+ return klass
+
+
+@python_2_unicode_compatible
class Book(models.Model):
title = models.CharField(max_length=50)
-
- def __unicode__(self):
+
+ def __str__(self):
return self.title
Please sign in to comment.
Something went wrong with that request. Please try again.