equal
deleted
inserted
replaced
1 """ |
|
2 2. Adding __str__() to models |
|
3 |
|
4 Although it's not a strict requirement, each model should have a ``__str__()`` |
|
5 method to return a "human-readable" representation of the object. Do this not |
|
6 only for your own sanity when dealing with the interactive prompt, but also |
|
7 because objects' representations are used throughout Django's |
|
8 automatically-generated admin. |
|
9 """ |
|
10 |
|
11 from django.db import models |
|
12 |
|
13 class Article(models.Model): |
|
14 headline = models.CharField(maxlength=100) |
|
15 pub_date = models.DateTimeField() |
|
16 |
|
17 def __str__(self): |
|
18 return self.headline |
|
19 |
|
20 __test__ = {'API_TESTS':""" |
|
21 # Create an Article. |
|
22 >>> from datetime import datetime |
|
23 >>> a = Article(headline='Area man programs in Python', pub_date=datetime(2005, 7, 28)) |
|
24 >>> a.save() |
|
25 |
|
26 >>> str(a) |
|
27 'Area man programs in Python' |
|
28 |
|
29 >>> a |
|
30 <Article: Area man programs in Python> |
|
31 """} |
|