summaryrefslogtreecommitdiff
path: root/tests/modeltests/str
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-07-07 13:23:57 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-07-07 13:23:57 +0000
commit1e747f98af99a23c5aff7b460d5b845c3a9e2013 (patch)
tree6347c27684a68cf1ef76b92808aa9309b0688797 /tests/modeltests/str
parent7ed1a919681b5b028c3f6eef1a862ff91b66feb2 (diff)
newforms-admin: Merged from trunk up to [5625]. This includes the Unicode
merge, however, not all of admin/ (and none of admindocs/) has been ported and checked yet. git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@5627 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/modeltests/str')
-rw-r--r--tests/modeltests/str/models.py36
1 files changed, 29 insertions, 7 deletions
diff --git a/tests/modeltests/str/models.py b/tests/modeltests/str/models.py
index 81230d538c..b4d3adcd6d 100644
--- a/tests/modeltests/str/models.py
+++ b/tests/modeltests/str/models.py
@@ -1,23 +1,39 @@
+# -*- coding: utf-8 -*-
"""
-2. Adding __str__() to models
+2. Adding __str__() or __unicode__() to models
-Although it's not a strict requirement, each model should have a ``__str__()``
-method to return a "human-readable" representation of the object. Do this not
-only for your own sanity when dealing with the interactive prompt, but also
-because objects' representations are used throughout Django's
-automatically-generated admin.
+Although it's not a strict requirement, each model should have a
+``_str__()`` or ``__unicode__()`` method to return a "human-readable"
+representation of the object. Do this not only for your own sanity when dealing
+with the interactive prompt, but also because objects' representations are used
+throughout Django's automatically-generated admin.
+
+Normally, you should write ``__unicode__``() method, since this will work for
+all field types (and Django will automatically provide an appropriate
+``__str__()`` method). However, you can write a ``__str__()`` method directly,
+if you prefer. You must be careful to encode the results correctly, though.
"""
from django.db import models
+from django.utils.encoding import smart_str
class Article(models.Model):
headline = models.CharField(maxlength=100)
pub_date = models.DateTimeField()
def __str__(self):
+ # Caution: this is only safe if you are certain that headline will be
+ # in ASCII.
+ return self.headline
+
+class InternationalArticle(models.Model):
+ headline = models.CharField(maxlength=100)
+ pub_date = models.DateTimeField()
+
+ def __unicode__(self):
return self.headline
-__test__ = {'API_TESTS':"""
+__test__ = {'API_TESTS':ur"""
# Create an Article.
>>> from datetime import datetime
>>> a = Article(headline='Area man programs in Python', pub_date=datetime(2005, 7, 28))
@@ -28,4 +44,10 @@ __test__ = {'API_TESTS':"""
>>> a
<Article: Area man programs in Python>
+
+>>> a1 = InternationalArticle(headline=u'Girl wins €12.500 in lottery', pub_date=datetime(2005, 7, 28))
+
+# The default str() output will be the UTF-8 encoded output of __unicode__().
+>>> str(a1)
+'Girl wins \xe2\x82\xac12.500 in lottery'
"""}