PageRenderTime 81ms CodeModel.GetById 40ms app.highlight 2ms RepoModel.GetById 38ms app.codeStats 0ms

/tests/modeltests/str/models.py

https://code.google.com/p/mango-py/
Python | 33 lines | 9 code | 1 blank | 23 comment | 5 complexity | b7596e82aa790dc0a73849ffc4e9f878 MD5 | raw file
 1# -*- coding: utf-8 -*-
 2"""
 32. Adding __str__() or __unicode__() to models
 4
 5Although it's not a strict requirement, each model should have a
 6``_str__()`` or ``__unicode__()`` method to return a "human-readable"
 7representation of the object. Do this not only for your own sanity when dealing
 8with the interactive prompt, but also because objects' representations are used
 9throughout Django's automatically-generated admin.
10
11Normally,  you should write ``__unicode__()`` method, since this will work for
12all field types (and Django will automatically provide an appropriate
13``__str__()`` method). However, you can write a ``__str__()`` method directly,
14if you prefer. You must be careful to encode the results correctly, though.
15"""
16
17from django.db import models
18
19class 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
28class InternationalArticle(models.Model):
29    headline = models.CharField(max_length=100)
30    pub_date = models.DateTimeField()
31
32    def __unicode__(self):
33        return self.headline