2005-07-29 23:15:40 +08:00
|
|
|
"""
|
|
|
|
2. Adding __repr__() to models
|
|
|
|
|
|
|
|
Although it's not a strict requirement, each model should have a ``__repr__()``
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2005-12-14 13:02:51 +08:00
|
|
|
from django.db import models
|
2005-07-29 23:15:40 +08:00
|
|
|
|
2005-12-14 13:02:51 +08:00
|
|
|
class Article(models.Model):
|
|
|
|
headline = models.CharField(maxlength=100)
|
|
|
|
pub_date = models.DateTimeField()
|
2005-07-29 23:15:40 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.headline
|
|
|
|
|
|
|
|
API_TESTS = """
|
|
|
|
# Create an Article.
|
|
|
|
>>> from datetime import datetime
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a = Article(headline='Area man programs in Python', pub_date=datetime(2005, 7, 28))
|
2005-07-29 23:15:40 +08:00
|
|
|
>>> a.save()
|
|
|
|
|
|
|
|
>>> repr(a)
|
|
|
|
'Area man programs in Python'
|
|
|
|
|
|
|
|
>>> a
|
|
|
|
Area man programs in Python
|
|
|
|
"""
|