equal
deleted
inserted
replaced
|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 2. Adding __str__() or __unicode__() to models |
|
4 |
|
5 Although it's not a strict requirement, each model should have a |
|
6 ``_str__()`` or ``__unicode__()`` method to return a "human-readable" |
|
7 representation of the object. Do this not only for your own sanity when dealing |
|
8 with the interactive prompt, but also because objects' representations are used |
|
9 throughout Django's automatically-generated admin. |
|
10 |
|
11 Normally, you should write ``__unicode__()`` method, since this will work for |
|
12 all field types (and Django will automatically provide an appropriate |
|
13 ``__str__()`` method). However, you can write a ``__str__()`` method directly, |
|
14 if you prefer. You must be careful to encode the results correctly, though. |
|
15 """ |
|
16 |
|
17 from django.db import models |
|
18 |
|
19 class Article(models.Model): |
|
20 headline = models.CharField(max_length=100) |
|
21 pub_date = models.DateTimeField() |
|
22 |
|
23 def __str__(self): |
|
24 # Caution: this is only safe if you are certain that headline will be |
|
25 # in ASCII. |
|
26 return self.headline |
|
27 |
|
28 class InternationalArticle(models.Model): |
|
29 headline = models.CharField(max_length=100) |
|
30 pub_date = models.DateTimeField() |
|
31 |
|
32 def __unicode__(self): |
|
33 return self.headline |