models.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. @python_2_unicode_compatible
  13. class Category(models.Model):
  14. name = models.CharField(max_length=20)
  15. class Meta:
  16. ordering = ('name',)
  17. def __str__(self):
  18. return self.name
  19. @python_2_unicode_compatible
  20. class Author(models.Model):
  21. name = models.CharField(max_length=20)
  22. class Meta:
  23. ordering = ('name',)
  24. def __str__(self):
  25. return self.name
  26. @python_2_unicode_compatible
  27. class Article(models.Model):
  28. author = models.ForeignKey(Author)
  29. headline = models.CharField(max_length=50)
  30. pub_date = models.DateTimeField()
  31. categories = models.ManyToManyField(Category)
  32. class Meta:
  33. ordering = ('pub_date',)
  34. def __str__(self):
  35. return self.headline
  36. @python_2_unicode_compatible
  37. class AuthorProfile(models.Model):
  38. author = models.OneToOneField(Author, primary_key=True)
  39. date_of_birth = models.DateField()
  40. def __str__(self):
  41. return "Profile of %s" % self.author
  42. @python_2_unicode_compatible
  43. class Actor(models.Model):
  44. name = models.CharField(max_length=20, primary_key=True)
  45. class Meta:
  46. ordering = ('name',)
  47. def __str__(self):
  48. return self.name
  49. @python_2_unicode_compatible
  50. class Movie(models.Model):
  51. actor = models.ForeignKey(Actor)
  52. title = models.CharField(max_length=50)
  53. price = models.DecimalField(max_digits=6, decimal_places=2, default=Decimal('0.00'))
  54. class Meta:
  55. ordering = ('title',)
  56. def __str__(self):
  57. return self.title
  58. class Score(models.Model):
  59. score = models.FloatField()
  60. @python_2_unicode_compatible
  61. class Team(object):
  62. def __init__(self, title):
  63. self.title = title
  64. def __str__(self):
  65. raise NotImplementedError("Not so simple")
  66. def to_string(self):
  67. return "%s" % self.title
  68. class TeamField(models.CharField):
  69. def __init__(self):
  70. super(TeamField, self).__init__(max_length=100)
  71. def get_db_prep_save(self, value, connection):
  72. return six.text_type(value.title)
  73. def to_python(self, value):
  74. if isinstance(value, Team):
  75. return value
  76. return Team(value)
  77. def from_db_value(self, value, connection, context):
  78. return Team(value)
  79. def value_to_string(self, obj):
  80. return self._get_val_from_obj(obj).to_string()
  81. def deconstruct(self):
  82. name, path, args, kwargs = super(TeamField, self).deconstruct()
  83. del kwargs['max_length']
  84. return name, path, args, kwargs
  85. @python_2_unicode_compatible
  86. class Player(models.Model):
  87. name = models.CharField(max_length=50)
  88. rank = models.IntegerField()
  89. team = TeamField()
  90. def __str__(self):
  91. return '%s (%d) playing for %s' % (self.name, self.rank, self.team.to_string())