models.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """
  2. The lookup API
  3. This demonstrates features of the database API.
  4. """
  5. from __future__ import unicode_literals
  6. from django.db import models
  7. from django.utils import six
  8. from django.utils.encoding import python_2_unicode_compatible
  9. class Alarm(models.Model):
  10. desc = models.CharField(max_length=100)
  11. time = models.TimeField()
  12. def __str__(self):
  13. return '%s (%s)' % (self.time, self.desc)
  14. class Author(models.Model):
  15. name = models.CharField(max_length=100)
  16. class Meta:
  17. ordering = ('name', )
  18. @python_2_unicode_compatible
  19. class Article(models.Model):
  20. headline = models.CharField(max_length=100)
  21. pub_date = models.DateTimeField()
  22. author = models.ForeignKey(Author, models.SET_NULL, blank=True, null=True)
  23. class Meta:
  24. ordering = ('-pub_date', 'headline')
  25. def __str__(self):
  26. return self.headline
  27. class Tag(models.Model):
  28. articles = models.ManyToManyField(Article)
  29. name = models.CharField(max_length=100)
  30. class Meta:
  31. ordering = ('name', )
  32. @python_2_unicode_compatible
  33. class Season(models.Model):
  34. year = models.PositiveSmallIntegerField()
  35. gt = models.IntegerField(null=True, blank=True)
  36. def __str__(self):
  37. return six.text_type(self.year)
  38. @python_2_unicode_compatible
  39. class Game(models.Model):
  40. season = models.ForeignKey(Season, models.CASCADE, related_name='games')
  41. home = models.CharField(max_length=100)
  42. away = models.CharField(max_length=100)
  43. def __str__(self):
  44. return "%s at %s" % (self.away, self.home)
  45. @python_2_unicode_compatible
  46. class Player(models.Model):
  47. name = models.CharField(max_length=100)
  48. games = models.ManyToManyField(Game, related_name='players')
  49. def __str__(self):
  50. return self.name
  51. # To test __search lookup a fulltext index is needed. This
  52. # is only available when using MySQL 5.6, or when using MyISAM
  53. # tables. As 5.6 isn't common yet, lets use MyISAM table for
  54. # testing. The table is manually created by the test method.
  55. # RemovedInDjango20Warning
  56. class MyISAMArticle(models.Model):
  57. headline = models.CharField(max_length=100)
  58. class Meta:
  59. db_table = 'myisam_article'
  60. managed = False