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
|
|
|
"""
|
2012-06-07 16:08:47 +00:00
|
|
|
from __future__ import unicode_literals
|
2006-06-28 16:00:37 +00:00
|
|
|
|
2010-05-04 14:00:30 +00:00
|
|
|
from decimal import Decimal
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2006-06-28 16:00:37 +00:00
|
|
|
from django.db import models
|
2012-07-20 12:48:51 +00:00
|
|
|
from django.utils import six
|
2012-08-12 10:32:08 +00:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2006-06-28 16:00:37 +00:00
|
|
|
|
2010-10-11 12:20:07 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2006-06-28 16:00:37 +00:00
|
|
|
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:
|
2013-09-03 18:22:21 +00:00
|
|
|
ordering = ('name',)
|
2006-06-28 16:00:37 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2006-06-28 16:00:37 +00:00
|
|
|
return self.name
|
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
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
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2006-06-28 16:00:37 +00:00
|
|
|
return self.name
|
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
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:
|
2013-09-03 18:22:21 +00:00
|
|
|
ordering = ('pub_date',)
|
2006-06-28 16:00:37 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2006-06-28 16:00:37 +00:00
|
|
|
return self.headline
|
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
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
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2012-06-07 16:08:47 +00: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 02:50:16 +00:00
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
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
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2007-09-15 05:32:29 +00: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 02:50:16 +00:00
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
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:
|
2013-09-03 18:22:21 +00:00
|
|
|
ordering = ('title',)
|
2007-09-15 05:32:29 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2007-09-15 05:32:29 +00: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 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
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2009-04-13 12:35:49 +00:00
|
|
|
class Team(object):
|
|
|
|
def __init__(self, title):
|
|
|
|
self.title = title
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2009-04-13 12:35:49 +00:00
|
|
|
raise NotImplementedError("Not so simple")
|
|
|
|
|
|
|
|
def to_string(self):
|
|
|
|
return "%s" % self.title
|
|
|
|
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2012-08-14 22:31:06 +00:00
|
|
|
class TeamField(six.with_metaclass(models.SubfieldBase, models.CharField)):
|
2009-04-13 12:35:49 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(TeamField, self).__init__(max_length=100)
|
|
|
|
|
2010-10-11 12:20:07 +00:00
|
|
|
def get_db_prep_save(self, value, connection):
|
2012-07-20 12:48:51 +00:00
|
|
|
return six.text_type(value.title)
|
2009-04-13 12:35:49 +00: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 14:04:34 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2009-04-13 12:35:49 +00:00
|
|
|
class Player(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
rank = models.IntegerField()
|
|
|
|
team = TeamField()
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2012-06-07 16:08:47 +00:00
|
|
|
return '%s (%d) playing for %s' % (self.name, self.rank, self.team.to_string())
|