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