2005-07-29 15:15:40 +00:00
|
|
|
"""
|
2006-06-03 22:14:04 +00:00
|
|
|
2. Adding __str__() to models
|
2005-07-29 15:15:40 +00:00
|
|
|
|
2006-06-03 22:14:04 +00:00
|
|
|
Although it's not a strict requirement, each model should have a ``__str__()``
|
2005-07-29 15:15:40 +00:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
from django.db import models
|
2005-07-29 15:15:40 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
class Article(models.Model):
|
|
|
|
headline = models.CharField(maxlength=100)
|
|
|
|
pub_date = models.DateTimeField()
|
2005-07-29 15:15:40 +00:00
|
|
|
|
2006-06-03 22:14:04 +00:00
|
|
|
def __str__(self):
|
2005-07-29 15:15:40 +00:00
|
|
|
return self.headline
|
|
|
|
|
2006-08-27 13:59:47 +00:00
|
|
|
__test__ = {'API_TESTS':"""
|
2005-07-29 15:15:40 +00:00
|
|
|
# Create an Article.
|
|
|
|
>>> from datetime import datetime
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a = Article(headline='Area man programs in Python', pub_date=datetime(2005, 7, 28))
|
2005-07-29 15:15:40 +00:00
|
|
|
>>> a.save()
|
|
|
|
|
2006-06-03 22:14:04 +00:00
|
|
|
>>> str(a)
|
2005-07-29 15:15:40 +00:00
|
|
|
'Area man programs in Python'
|
|
|
|
|
|
|
|
>>> a
|
2006-06-03 22:14:04 +00:00
|
|
|
<Article: Area man programs in Python>
|
2006-08-27 13:59:47 +00:00
|
|
|
"""}
|