2007-09-15 13:32:29 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2006-06-29 00:00:37 +08:00
|
|
|
"""
|
2007-12-02 05:18:45 +08:00
|
|
|
42. Serialization
|
2006-06-29 00:00:37 +08:00
|
|
|
|
2008-08-12 22:15:38 +08:00
|
|
|
``django.core.serializers`` provides interfaces to converting Django
|
|
|
|
``QuerySet`` objects to and from "flat" data (i.e. strings).
|
2006-06-29 00:00:37 +08:00
|
|
|
"""
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
2006-06-29 00:00:37 +08:00
|
|
|
|
2010-05-04 22:00:30 +08:00
|
|
|
from decimal import Decimal
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
from django.db import models
|
2012-07-20 20:48:51 +08:00
|
|
|
from django.utils import six
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2006-06-29 00:00:37 +08:00
|
|
|
|
2010-10-11 20:20:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2006-06-29 00:00:37 +08:00
|
|
|
class Category(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=20)
|
2006-06-29 00:00:37 +08:00
|
|
|
|
|
|
|
class Meta:
|
2013-09-04 02:22:21 +08:00
|
|
|
ordering = ('name',)
|
2006-06-29 00:00:37 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-06-29 00:00:37 +08:00
|
|
|
return self.name
|
|
|
|
|
2010-09-19 22:04:34 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2006-06-29 00:00:37 +08:00
|
|
|
class Author(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=20)
|
2006-06-29 00:00:37 +08: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 10:50:16 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-06-29 00:00:37 +08:00
|
|
|
return self.name
|
|
|
|
|
2010-09-19 22:04:34 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2006-06-29 00:00:37 +08:00
|
|
|
class Article(models.Model):
|
|
|
|
author = models.ForeignKey(Author)
|
2007-08-05 13:14:46 +08:00
|
|
|
headline = models.CharField(max_length=50)
|
2006-06-29 00:00:37 +08:00
|
|
|
pub_date = models.DateTimeField()
|
|
|
|
categories = models.ManyToManyField(Category)
|
|
|
|
|
|
|
|
class Meta:
|
2013-09-04 02:22:21 +08:00
|
|
|
ordering = ('pub_date',)
|
2006-06-29 00:00:37 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-06-29 00:00:37 +08:00
|
|
|
return self.headline
|
|
|
|
|
2010-09-19 22:04:34 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2007-01-27 21:42:51 +08: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 10:50:16 +08:00
|
|
|
author = models.OneToOneField(Author, primary_key=True)
|
2007-01-27 21:42:51 +08: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 10:50:16 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "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 10:50:16 +08:00
|
|
|
|
2010-09-19 22:04:34 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2007-09-15 13:32:29 +08: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 10:50:16 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2007-09-15 13:32:29 +08:00
|
|
|
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 10:50:16 +08:00
|
|
|
|
2010-09-19 22:04:34 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2007-09-15 13:32:29 +08:00
|
|
|
class Movie(models.Model):
|
|
|
|
actor = models.ForeignKey(Actor)
|
|
|
|
title = models.CharField(max_length=50)
|
2009-02-12 04:13:17 +08:00
|
|
|
price = models.DecimalField(max_digits=6, decimal_places=2, default=Decimal('0.00'))
|
2007-09-15 13:32:29 +08:00
|
|
|
|
|
|
|
class Meta:
|
2013-09-04 02:22:21 +08:00
|
|
|
ordering = ('title',)
|
2007-09-15 13:32:29 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2007-09-15 13:32:29 +08:00
|
|
|
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 10:50:16 +08:00
|
|
|
|
2010-09-19 22:04:34 +08:00
|
|
|
|
2007-09-22 21:21:54 +08:00
|
|
|
class Score(models.Model):
|
|
|
|
score = models.FloatField()
|
2007-01-27 21:42:51 +08:00
|
|
|
|
2009-04-13 20:35:49 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-04-13 20:35:49 +08:00
|
|
|
class Team(object):
|
|
|
|
def __init__(self, title):
|
|
|
|
self.title = title
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-04-13 20:35:49 +08:00
|
|
|
raise NotImplementedError("Not so simple")
|
|
|
|
|
|
|
|
def to_string(self):
|
|
|
|
return "%s" % self.title
|
|
|
|
|
2010-09-19 22:04:34 +08:00
|
|
|
|
2012-08-15 06:31:06 +08:00
|
|
|
class TeamField(six.with_metaclass(models.SubfieldBase, models.CharField)):
|
2009-04-13 20:35:49 +08:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(TeamField, self).__init__(max_length=100)
|
|
|
|
|
2010-10-11 20:20:07 +08:00
|
|
|
def get_db_prep_save(self, value, connection):
|
2012-07-20 20:48:51 +08:00
|
|
|
return six.text_type(value.title)
|
2009-04-13 20:35:49 +08:00
|
|
|
|
|
|
|
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 22:04:34 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-04-13 20:35:49 +08:00
|
|
|
class Player(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
rank = models.IntegerField()
|
|
|
|
team = TeamField()
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return '%s (%d) playing for %s' % (self.name, self.rank, self.team.to_string())
|