Snippet 6: extending or specifying Model Instances

Refer to documentation here

More specifically for def __str__(self):

“The __str__() method is called whenever you call str() on an object. The main use for this method directly inside Django is when the repr() output of a model is displayed anywhere (for example, in debugging output). Thus, you should return a nice, human-readable string for the object’s __str__().” 

Example:

Screen Shot 2013-02-18 at 3.02.43 PM

Error: if you put an integer here it will throw a type error!

Screen Shot 2013-02-18 at 3.04.43 PM

The error would look like:

TypeError at /admin/
__str__ returned non-string (type datetime.date)

Solution is to use python’s conversion to string method  str():

Screen Shot 2013-02-18 at 3.04.59 PM

Advertisements
This entry was posted in Snippets. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s