PageRenderTime 19ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/str/models.py

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