2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2009-04-09 02:53:55 +08:00
|
|
|
import os
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2010-02-10 08:34:45 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
2011-10-14 05:34:56 +08:00
|
|
|
from django.db import models
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2012-12-08 18:13:52 +08:00
|
|
|
from django.utils._os import upath
|
2010-02-10 08:34:45 +08:00
|
|
|
|
2009-04-18 23:51:11 +08:00
|
|
|
|
|
|
|
class Person(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2008-10-08 18:09:44 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2008-10-08 18:09:44 +08:00
|
|
|
class Triple(models.Model):
|
|
|
|
left = models.IntegerField()
|
|
|
|
middle = models.IntegerField()
|
|
|
|
right = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
2012-06-08 00:08:47 +08:00
|
|
|
unique_together = (('left', 'middle'), ('middle', 'right'))
|
2008-10-08 18:09:44 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2009-04-09 02:53:55 +08:00
|
|
|
class FilePathModel(models.Model):
|
2012-12-08 18:13:52 +08:00
|
|
|
path = models.FilePathField(path=os.path.dirname(upath(__file__)), match=".*\.py$", blank=True)
|
2009-05-02 15:03:33 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-05-02 15:03:33 +08:00
|
|
|
class Publication(models.Model):
|
|
|
|
title = models.CharField(max_length=30)
|
2009-05-07 22:48:04 +08:00
|
|
|
date_published = models.DateField()
|
2009-05-02 15:03:33 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-05-02 15:03:33 +08:00
|
|
|
return self.title
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-05-02 15:03:33 +08:00
|
|
|
class Article(models.Model):
|
|
|
|
headline = models.CharField(max_length=100)
|
|
|
|
publications = models.ManyToManyField(Publication)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-05-02 15:03:33 +08:00
|
|
|
return self.headline
|
2009-05-20 07:13:33 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2009-05-20 07:13:33 +08:00
|
|
|
class CustomFileField(models.FileField):
|
|
|
|
def save_form_data(self, instance, data):
|
|
|
|
been_here = getattr(self, 'been_saved', False)
|
|
|
|
assert not been_here, "save_form_data called more than once"
|
|
|
|
setattr(self, 'been_saved', True)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2009-05-20 07:13:33 +08:00
|
|
|
class CustomFF(models.Model):
|
|
|
|
f = CustomFileField(upload_to='unused', blank=True)
|
2010-02-10 08:34:45 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2010-02-10 08:34:45 +08:00
|
|
|
class RealPerson(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
if self.name.lower() == 'anonymous':
|
|
|
|
raise ValidationError("Please specify a real name.")
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2010-02-24 04:02:18 +08:00
|
|
|
class Author(models.Model):
|
|
|
|
publication = models.OneToOneField(Publication, null=True, blank=True)
|
|
|
|
full_name = models.CharField(max_length=255)
|
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2010-02-24 04:02:18 +08:00
|
|
|
class Author1(models.Model):
|
|
|
|
publication = models.OneToOneField(Publication, null=False)
|
|
|
|
full_name = models.CharField(max_length=255)
|
2010-08-05 11:59:20 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2010-08-05 11:59:20 +08:00
|
|
|
class Homepage(models.Model):
|
2012-03-31 21:55:03 +08:00
|
|
|
url = models.URLField()
|
2010-10-01 10:02:58 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2010-10-01 10:02:58 +08:00
|
|
|
class Document(models.Model):
|
|
|
|
myfile = models.FileField(upload_to='unused', blank=True)
|
2010-10-13 12:36:51 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2010-10-13 12:36:51 +08:00
|
|
|
class Edition(models.Model):
|
|
|
|
author = models.ForeignKey(Person)
|
|
|
|
publication = models.ForeignKey(Publication)
|
|
|
|
edition = models.IntegerField()
|
|
|
|
isbn = models.CharField(max_length=13, unique=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (('author', 'publication'), ('publication', 'edition'),)
|