2007-09-15 05:32:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2006-06-28 16:00:37 +00:00
|
|
|
"""
|
2007-12-01 21:18:45 +00:00
|
|
|
42. Serialization
|
2006-06-28 16:00:37 +00:00
|
|
|
|
2008-08-12 14:15:38 +00:00
|
|
|
``django.core.serializers`` provides interfaces to converting Django
|
|
|
|
``QuerySet`` objects to and from "flat" data (i.e. strings).
|
2006-06-28 16:00:37 +00:00
|
|
|
"""
|
|
|
|
|
2010-05-04 14:00:30 +00:00
|
|
|
from decimal import Decimal
|
2006-06-28 16:00:37 +00:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Category(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=20)
|
2006-06-28 16:00:37 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
2006-06-28 16:00:37 +00:00
|
|
|
return self.name
|
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2006-06-28 16:00:37 +00:00
|
|
|
class Author(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=20)
|
2006-06-28 16:00:37 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 02:50:16 +00:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
2006-06-28 16:00:37 +00:00
|
|
|
return self.name
|
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2006-06-28 16:00:37 +00:00
|
|
|
class Article(models.Model):
|
|
|
|
author = models.ForeignKey(Author)
|
2007-08-05 05:14:46 +00:00
|
|
|
headline = models.CharField(max_length=50)
|
2006-06-28 16:00:37 +00:00
|
|
|
pub_date = models.DateTimeField()
|
|
|
|
categories = models.ManyToManyField(Category)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('pub_date',)
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
2006-06-28 16:00:37 +00:00
|
|
|
return self.headline
|
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2007-01-27 13:42:51 +00:00
|
|
|
class AuthorProfile(models.Model):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 02:50:16 +00:00
|
|
|
author = models.OneToOneField(Author, primary_key=True)
|
2007-01-27 13:42:51 +00:00
|
|
|
date_of_birth = models.DateField()
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 02:50:16 +00:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"Profile of %s" % self.author
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 02:50:16 +00:00
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2007-09-15 05:32:29 +00:00
|
|
|
class Actor(models.Model):
|
|
|
|
name = models.CharField(max_length=20, primary_key=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 02:50:16 +00:00
|
|
|
|
2007-09-15 05:32:29 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 02:50:16 +00:00
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2007-09-15 05:32:29 +00:00
|
|
|
class Movie(models.Model):
|
|
|
|
actor = models.ForeignKey(Actor)
|
|
|
|
title = models.CharField(max_length=50)
|
2009-02-11 20:13:17 +00:00
|
|
|
price = models.DecimalField(max_digits=6, decimal_places=2, default=Decimal('0.00'))
|
2007-09-15 05:32:29 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('title',)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 02:50:16 +00:00
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2007-09-22 13:21:54 +00:00
|
|
|
class Score(models.Model):
|
|
|
|
score = models.FloatField()
|
2007-01-27 13:42:51 +00:00
|
|
|
|
2009-04-13 12:35:49 +00:00
|
|
|
|
|
|
|
class Team(object):
|
|
|
|
def __init__(self, title):
|
|
|
|
self.title = title
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
raise NotImplementedError("Not so simple")
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
raise NotImplementedError("Not so simple")
|
|
|
|
|
|
|
|
def to_string(self):
|
|
|
|
return "%s" % self.title
|
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2009-04-13 12:35:49 +00:00
|
|
|
class TeamField(models.CharField):
|
|
|
|
__metaclass__ = models.SubfieldBase
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(TeamField, self).__init__(max_length=100)
|
|
|
|
|
|
|
|
def get_db_prep_save(self, value):
|
|
|
|
return unicode(value.title)
|
|
|
|
|
|
|
|
def to_python(self, value):
|
|
|
|
if isinstance(value, Team):
|
|
|
|
return value
|
|
|
|
return Team(value)
|
|
|
|
|
|
|
|
def value_to_string(self, obj):
|
|
|
|
return self._get_val_from_obj(obj).to_string()
|
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2009-04-13 12:35:49 +00:00
|
|
|
class Player(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
rank = models.IntegerField()
|
|
|
|
team = TeamField()
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u'%s (%d) playing for %s' % (self.name, self.rank, self.team.to_string())
|