PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 0ms

/tests/modeltests/serializers/models.py

https://code.google.com/p/mango-py/
Python | 118 lines | 113 code | 0 blank | 5 comment | 0 complexity | 5eee04e15ec196696c1d6c9f1288a805 MD5 | raw file
  1# -*- coding: utf-8 -*-
  2"""
  342. Serialization
  4
  5``django.core.serializers`` provides interfaces to converting Django
  6``QuerySet`` objects to and from "flat" data (i.e. strings).
  7"""
  8
  9from decimal import Decimal
 10from django.db import models
 11
 12
 13class Category(models.Model):
 14    name = models.CharField(max_length=20)
 15
 16    class Meta:
 17       ordering = ('name',)
 18
 19    def __unicode__(self):
 20        return self.name
 21
 22
 23class Author(models.Model):
 24    name = models.CharField(max_length=20)
 25
 26    class Meta:
 27        ordering = ('name',)
 28
 29    def __unicode__(self):
 30        return self.name
 31
 32
 33class Article(models.Model):
 34    author = models.ForeignKey(Author)
 35    headline = models.CharField(max_length=50)
 36    pub_date = models.DateTimeField()
 37    categories = models.ManyToManyField(Category)
 38
 39    class Meta:
 40       ordering = ('pub_date',)
 41
 42    def __unicode__(self):
 43        return self.headline
 44
 45
 46class AuthorProfile(models.Model):
 47    author = models.OneToOneField(Author, primary_key=True)
 48    date_of_birth = models.DateField()
 49
 50    def __unicode__(self):
 51        return u"Profile of %s" % self.author
 52
 53
 54class Actor(models.Model):
 55    name = models.CharField(max_length=20, primary_key=True)
 56
 57    class Meta:
 58        ordering = ('name',)
 59
 60    def __unicode__(self):
 61        return self.name
 62
 63
 64class Movie(models.Model):
 65    actor = models.ForeignKey(Actor)
 66    title = models.CharField(max_length=50)
 67    price = models.DecimalField(max_digits=6, decimal_places=2, default=Decimal('0.00'))
 68
 69    class Meta:
 70       ordering = ('title',)
 71
 72    def __unicode__(self):
 73        return self.title
 74
 75
 76class Score(models.Model):
 77    score = models.FloatField()
 78
 79
 80class Team(object):
 81    def __init__(self, title):
 82        self.title = title
 83
 84    def __unicode__(self):
 85        raise NotImplementedError("Not so simple")
 86
 87    def __str__(self):
 88        raise NotImplementedError("Not so simple")
 89
 90    def to_string(self):
 91        return "%s" % self.title
 92
 93
 94class TeamField(models.CharField):
 95    __metaclass__ = models.SubfieldBase
 96
 97    def __init__(self):
 98        super(TeamField, self).__init__(max_length=100)
 99
100    def get_db_prep_save(self, value, connection):
101        return unicode(value.title)
102
103    def to_python(self, value):
104        if isinstance(value, Team):
105            return value
106        return Team(value)
107
108    def value_to_string(self, obj):
109        return self._get_val_from_obj(obj).to_string()
110
111
112class Player(models.Model):
113    name = models.CharField(max_length=50)
114    rank = models.IntegerField()
115    team = TeamField()
116
117    def __unicode__(self):
118        return u'%s (%d) playing for %s' % (self.name, self.rank, self.team.to_string())