2007-09-15 05:32:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2006-06-28 16:00:37 +00:00
|
|
|
"""
|
2014-09-24 05:13:13 +00:00
|
|
|
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
|
|
|
|
2015-03-26 18:31:09 +00:00
|
|
|
class CategoryMetaDataManager(models.Manager):
|
|
|
|
|
|
|
|
def get_by_natural_key(self, kind, name):
|
|
|
|
return self.get(kind=kind, name=name)
|
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class CategoryMetaData(models.Model):
|
|
|
|
kind = models.CharField(max_length=10)
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
value = models.CharField(max_length=10)
|
|
|
|
objects = CategoryMetaDataManager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (('kind', 'name'),)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '[%s:%s]=%s' % (self.kind, self.name, self.value)
|
|
|
|
|
|
|
|
def natural_key(self):
|
|
|
|
return (self.kind, self.name)
|
|
|
|
|
|
|
|
|
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)
|
2015-07-22 14:43:21 +00:00
|
|
|
meta_data = models.ForeignKey(CategoryMetaData, models.SET_NULL, null=True, default=None)
|
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):
|
2015-07-22 14:43:21 +00:00
|
|
|
author = models.ForeignKey(Author, models.CASCADE)
|
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)
|
2015-03-26 18:31:09 +00:00
|
|
|
meta_data = models.ManyToManyField(CategoryMetaData)
|
2006-06-28 16:00:37 +00:00
|
|
|
|
|
|
|
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):
|
2015-07-22 14:43:21 +00:00
|
|
|
author = models.OneToOneField(Author, models.CASCADE, 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):
|
2015-07-22 14:43:21 +00:00
|
|
|
actor = models.ForeignKey(Actor, models.CASCADE)
|
2007-09-15 05:32:29 +00:00
|
|
|
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
|
|
|
|
2014-08-12 12:08:40 +00:00
|
|
|
class TeamField(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)
|
|
|
|
|
2015-02-20 10:53:59 +00:00
|
|
|
def from_db_value(self, value, expression, connection, context):
|
2014-08-12 12:08:40 +00:00
|
|
|
return Team(value)
|
|
|
|
|
2009-04-13 12:35:49 +00:00
|
|
|
def value_to_string(self, obj):
|
2015-04-26 06:30:46 +00:00
|
|
|
return self.value_from_object(obj).to_string()
|
2009-04-13 12:35:49 +00:00
|
|
|
|
2013-12-06 14:21:13 +00:00
|
|
|
def deconstruct(self):
|
|
|
|
name, path, args, kwargs = super(TeamField, self).deconstruct()
|
|
|
|
del kwargs['max_length']
|
|
|
|
return name, path, args, kwargs
|
|
|
|
|
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())
|