models.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # -*- coding: utf-8 -*-
  2. """
  3. Serialization
  4. ``django.core.serializers`` provides interfaces to converting Django
  5. ``QuerySet`` objects to and from "flat" data (i.e. strings).
  6. """
  7. from __future__ import unicode_literals
  8. from decimal import Decimal
  9. from django.db import models
  10. from django.utils import six
  11. from django.utils.encoding import python_2_unicode_compatible
  12. class CategoryMetaDataManager(models.Manager):
  13. def get_by_natural_key(self, kind, name):
  14. return self.get(kind=kind, name=name)
  15. @python_2_unicode_compatible
  16. class CategoryMetaData(models.Model):
  17. kind = models.CharField(max_length=10)
  18. name = models.CharField(max_length=10)
  19. value = models.CharField(max_length=10)
  20. objects = CategoryMetaDataManager()
  21. class Meta:
  22. unique_together = (('kind', 'name'),)
  23. def __str__(self):
  24. return '[%s:%s]=%s' % (self.kind, self.name, self.value)
  25. def natural_key(self):
  26. return (self.kind, self.name)
  27. @python_2_unicode_compatible
  28. class Category(models.Model):
  29. name = models.CharField(max_length=20)
  30. meta_data = models.ForeignKey(CategoryMetaData, null=True, default=None)
  31. class Meta:
  32. ordering = ('name',)
  33. def __str__(self):
  34. return self.name
  35. @python_2_unicode_compatible
  36. class Author(models.Model):
  37. name = models.CharField(max_length=20)
  38. class Meta:
  39. ordering = ('name',)
  40. def __str__(self):
  41. return self.name
  42. @python_2_unicode_compatible
  43. class Article(models.Model):
  44. author = models.ForeignKey(Author)
  45. headline = models.CharField(max_length=50)
  46. pub_date = models.DateTimeField()
  47. categories = models.ManyToManyField(Category)
  48. meta_data = models.ManyToManyField(CategoryMetaData)
  49. class Meta:
  50. ordering = ('pub_date',)
  51. def __str__(self):
  52. return self.headline
  53. @python_2_unicode_compatible
  54. class AuthorProfile(models.Model):
  55. author = models.OneToOneField(Author, primary_key=True)
  56. date_of_birth = models.DateField()
  57. def __str__(self):
  58. return "Profile of %s" % self.author
  59. @python_2_unicode_compatible
  60. class Actor(models.Model):
  61. name = models.CharField(max_length=20, primary_key=True)
  62. class Meta:
  63. ordering = ('name',)
  64. def __str__(self):
  65. return self.name
  66. @python_2_unicode_compatible
  67. class Movie(models.Model):
  68. actor = models.ForeignKey(Actor)
  69. title = models.CharField(max_length=50)
  70. price = models.DecimalField(max_digits=6, decimal_places=2, default=Decimal('0.00'))
  71. class Meta:
  72. ordering = ('title',)
  73. def __str__(self):
  74. return self.title
  75. class Score(models.Model):
  76. score = models.FloatField()
  77. @python_2_unicode_compatible
  78. class Team(object):
  79. def __init__(self, title):
  80. self.title = title
  81. def __str__(self):
  82. raise NotImplementedError("Not so simple")
  83. def to_string(self):
  84. return "%s" % self.title
  85. class TeamField(models.CharField):
  86. def __init__(self):
  87. super(TeamField, self).__init__(max_length=100)
  88. def get_db_prep_save(self, value, connection):
  89. return six.text_type(value.title)
  90. def to_python(self, value):
  91. if isinstance(value, Team):
  92. return value
  93. return Team(value)
  94. def from_db_value(self, value, expression, connection, context):
  95. return Team(value)
  96. def value_to_string(self, obj):
  97. return self.value_from_object(obj).to_string()
  98. def deconstruct(self):
  99. name, path, args, kwargs = super(TeamField, self).deconstruct()
  100. del kwargs['max_length']
  101. return name, path, args, kwargs
  102. @python_2_unicode_compatible
  103. class Player(models.Model):
  104. name = models.CharField(max_length=50)
  105. rank = models.IntegerField()
  106. team = TeamField()
  107. def __str__(self):
  108. return '%s (%d) playing for %s' % (self.name, self.rank, self.team.to_string())