2014-02-01 19:23:31 +00:00
|
|
|
import datetime
|
2011-11-13 19:05:02 +00:00
|
|
|
import os
|
2008-01-17 18:03:21 +00:00
|
|
|
import tempfile
|
2015-08-06 21:43:55 +00:00
|
|
|
import uuid
|
2008-01-17 18:03:21 +00:00
|
|
|
|
2013-06-05 18:55:05 +00:00
|
|
|
from django.core import validators
|
2014-03-21 14:54:53 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2008-08-08 20:59:02 +00:00
|
|
|
from django.core.files.storage import FileSystemStorage
|
2011-10-13 18:04:12 +00:00
|
|
|
from django.db import models
|
2008-08-08 20:59:02 +00:00
|
|
|
|
2015-02-21 17:56:36 +00:00
|
|
|
temp_storage_dir = tempfile.mkdtemp()
|
2008-10-10 22:13:16 +00:00
|
|
|
temp_storage = FileSystemStorage(temp_storage_dir)
|
2006-12-15 05:46:11 +00:00
|
|
|
|
2007-04-28 14:18:03 +00:00
|
|
|
ARTICLE_STATUS = (
|
|
|
|
(1, 'Draft'),
|
|
|
|
(2, 'Pending'),
|
|
|
|
(3, 'Live'),
|
|
|
|
)
|
|
|
|
|
2008-08-31 20:11:11 +00:00
|
|
|
ARTICLE_STATUS_CHAR = (
|
|
|
|
('d', 'Draft'),
|
|
|
|
('p', 'Pending'),
|
|
|
|
('l', 'Live'),
|
|
|
|
)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2014-03-14 14:18:08 +00:00
|
|
|
class Person(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
|
2006-12-15 05:46:11 +00:00
|
|
|
class Category(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=20)
|
2007-09-08 05:09:39 +00:00
|
|
|
slug = models.SlugField(max_length=20)
|
2007-08-05 05:14:46 +00:00
|
|
|
url = models.CharField('The URL', max_length=40)
|
2006-12-15 05:46:11 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2006-12-15 05:46:11 +00:00
|
|
|
return self.name
|
|
|
|
|
2012-08-14 21:38:35 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return self.__str__()
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-06-10 10:22:40 +00:00
|
|
|
class Writer(models.Model):
|
|
|
|
name = models.CharField(max_length=50, help_text='Use both first and last names.')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2006-12-15 05:46:11 +00:00
|
|
|
class Article(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
headline = models.CharField(max_length=50)
|
2007-09-08 05:09:39 +00:00
|
|
|
slug = models.SlugField()
|
2006-12-30 00:12:02 +00:00
|
|
|
pub_date = models.DateField()
|
2007-02-20 02:59:16 +00:00
|
|
|
created = models.DateField(editable=False)
|
2015-07-22 14:43:21 +00:00
|
|
|
writer = models.ForeignKey(Writer, models.CASCADE)
|
2007-01-10 23:34:37 +00:00
|
|
|
article = models.TextField()
|
2006-12-28 02:34:53 +00:00
|
|
|
categories = models.ManyToManyField(Category, blank=True)
|
2008-09-01 19:20:03 +00:00
|
|
|
status = models.PositiveIntegerField(choices=ARTICLE_STATUS, blank=True, null=True)
|
2006-12-15 05:46:11 +00:00
|
|
|
|
2014-03-14 08:44:56 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2007-02-20 02:59:16 +00:00
|
|
|
if not self.id:
|
|
|
|
self.created = datetime.date.today()
|
2017-01-21 13:13:44 +00:00
|
|
|
return super().save(*args, **kwargs)
|
2007-02-20 02:59:16 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2006-12-15 05:46:11 +00:00
|
|
|
return self.headline
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2008-08-22 19:27:26 +00:00
|
|
|
class ImprovedArticle(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
article = models.OneToOneField(Article, models.CASCADE)
|
2008-08-22 19:27:26 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2008-08-22 19:27:26 +00:00
|
|
|
class ImprovedArticleWithParentLink(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
article = models.OneToOneField(Article, models.CASCADE, parent_link=True)
|
2008-08-22 19:27:26 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-06-10 10:22:40 +00:00
|
|
|
class BetterWriter(Writer):
|
2010-01-05 03:56:19 +00:00
|
|
|
score = models.IntegerField()
|
2008-08-25 03:51:25 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2014-03-14 14:18:08 +00:00
|
|
|
class Publication(models.Model):
|
|
|
|
title = models.CharField(max_length=30)
|
|
|
|
date_published = models.DateField()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
|
2015-03-04 19:19:02 +00:00
|
|
|
def default_mode():
|
|
|
|
return 'di'
|
|
|
|
|
|
|
|
|
|
|
|
def default_category():
|
|
|
|
return 3
|
|
|
|
|
|
|
|
|
|
|
|
class PublicationDefaults(models.Model):
|
|
|
|
MODE_CHOICES = (('di', 'direct'), ('de', 'delayed'))
|
|
|
|
CATEGORY_CHOICES = ((1, 'Games'), (2, 'Comics'), (3, 'Novel'))
|
|
|
|
title = models.CharField(max_length=30)
|
|
|
|
date_published = models.DateField(default=datetime.date.today)
|
2016-09-06 21:41:54 +00:00
|
|
|
datetime_published = models.DateTimeField(default=datetime.datetime(2000, 1, 1))
|
2015-03-04 19:19:02 +00:00
|
|
|
mode = models.CharField(max_length=2, choices=MODE_CHOICES, default=default_mode)
|
|
|
|
category = models.IntegerField(choices=CATEGORY_CHOICES, default=default_category)
|
2016-08-11 19:18:48 +00:00
|
|
|
active = models.BooleanField(default=True)
|
2016-09-06 21:41:54 +00:00
|
|
|
file = models.FileField(default='default.txt')
|
2015-03-04 19:19:02 +00:00
|
|
|
|
|
|
|
|
2014-03-14 14:18:08 +00:00
|
|
|
class Author(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
publication = models.OneToOneField(Publication, models.SET_NULL, null=True, blank=True)
|
2014-03-14 14:18:08 +00:00
|
|
|
full_name = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
|
|
|
|
class Author1(models.Model):
|
2016-03-18 14:24:29 +00:00
|
|
|
publication = models.OneToOneField(Publication, models.CASCADE, null=False)
|
2014-03-14 14:18:08 +00:00
|
|
|
full_name = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
|
2013-06-10 10:22:40 +00:00
|
|
|
class WriterProfile(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
writer = models.OneToOneField(Writer, models.CASCADE, primary_key=True)
|
2008-08-31 09:49:55 +00:00
|
|
|
age = models.PositiveIntegerField()
|
2008-09-01 22:43:38 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-08-31 09:49:55 +00:00
|
|
|
return "%s is %s" % (self.writer, self.age)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2014-03-14 14:18:08 +00:00
|
|
|
class Document(models.Model):
|
|
|
|
myfile = models.FileField(upload_to='unused', blank=True)
|
|
|
|
|
|
|
|
|
2008-01-17 18:03:21 +00:00
|
|
|
class TextFile(models.Model):
|
|
|
|
description = models.CharField(max_length=20)
|
2009-03-30 22:52:16 +00:00
|
|
|
file = models.FileField(storage=temp_storage, upload_to='tests', max_length=15)
|
2008-01-17 18:03:21 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-01-17 18:03:21 +00:00
|
|
|
return self.description
|
2008-02-14 12:56:49 +00:00
|
|
|
|
2014-03-14 14:18:08 +00: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)
|
|
|
|
|
|
|
|
|
|
|
|
class CustomFF(models.Model):
|
|
|
|
f = CustomFileField(upload_to='unused', blank=True)
|
|
|
|
|
|
|
|
|
|
|
|
class FilePathModel(models.Model):
|
2018-02-28 15:24:19 +00:00
|
|
|
path = models.FilePathField(path=os.path.dirname(__file__), match='models.py', blank=True)
|
2014-03-14 14:18:08 +00:00
|
|
|
|
|
|
|
|
2009-02-16 18:34:28 +00:00
|
|
|
try:
|
2014-03-21 14:54:53 +00:00
|
|
|
from PIL import Image # NOQA: detect if Pillow is installed
|
2010-03-16 19:32:11 +00:00
|
|
|
|
2009-02-16 18:34:28 +00:00
|
|
|
test_images = True
|
2009-03-15 05:05:26 +00:00
|
|
|
|
2009-02-16 18:34:28 +00:00
|
|
|
class ImageFile(models.Model):
|
|
|
|
def custom_upload_path(self, filename):
|
|
|
|
path = self.path or 'tests'
|
|
|
|
return '%s/%s' % (path, filename)
|
2009-03-15 05:05:26 +00:00
|
|
|
|
2009-02-16 18:34:28 +00:00
|
|
|
description = models.CharField(max_length=20)
|
2009-05-11 09:57:19 +00:00
|
|
|
|
|
|
|
# Deliberately put the image field *after* the width/height fields to
|
|
|
|
# trigger the bug in #10404 with width/height not getting assigned.
|
2009-02-16 18:34:28 +00:00
|
|
|
width = models.IntegerField(editable=False)
|
|
|
|
height = models.IntegerField(editable=False)
|
2009-05-11 09:57:19 +00:00
|
|
|
image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
|
|
|
|
width_field='width', height_field='height')
|
2009-02-16 18:34:28 +00:00
|
|
|
path = models.CharField(max_length=16, blank=True, default='')
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2009-02-16 18:34:28 +00:00
|
|
|
return self.description
|
2009-03-15 05:05:26 +00:00
|
|
|
|
2009-02-16 18:34:28 +00:00
|
|
|
class OptionalImageFile(models.Model):
|
|
|
|
def custom_upload_path(self, filename):
|
|
|
|
path = self.path or 'tests'
|
|
|
|
return '%s/%s' % (path, filename)
|
2009-03-15 05:05:26 +00:00
|
|
|
|
2009-02-16 18:34:28 +00:00
|
|
|
description = models.CharField(max_length=20)
|
|
|
|
image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
|
2009-03-15 05:05:26 +00:00
|
|
|
width_field='width', height_field='height',
|
2009-02-16 18:34:28 +00:00
|
|
|
blank=True, null=True)
|
|
|
|
width = models.IntegerField(editable=False, null=True)
|
|
|
|
height = models.IntegerField(editable=False, null=True)
|
|
|
|
path = models.CharField(max_length=16, blank=True, default='')
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2009-02-16 18:34:28 +00:00
|
|
|
return self.description
|
2017-05-28 07:05:21 +00:00
|
|
|
|
|
|
|
class NoExtensionImageFile(models.Model):
|
|
|
|
def upload_to(self, filename):
|
|
|
|
return 'tests/no_extension'
|
|
|
|
|
|
|
|
description = models.CharField(max_length=20)
|
|
|
|
image = models.ImageField(storage=temp_storage, upload_to=upload_to)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.description
|
|
|
|
|
2014-03-21 14:54:53 +00:00
|
|
|
except ImportError:
|
2009-02-16 18:34:28 +00:00
|
|
|
test_images = False
|
2009-03-15 05:05:26 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2014-03-14 14:18:08 +00:00
|
|
|
class Homepage(models.Model):
|
|
|
|
url = models.URLField()
|
|
|
|
|
|
|
|
|
2008-09-01 19:08:08 +00:00
|
|
|
class Product(models.Model):
|
|
|
|
slug = models.SlugField(unique=True)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-09-01 19:08:08 +00:00
|
|
|
return self.slug
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2008-09-01 19:08:08 +00:00
|
|
|
class Price(models.Model):
|
|
|
|
price = models.DecimalField(max_digits=10, decimal_places=2)
|
|
|
|
quantity = models.PositiveIntegerField()
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2012-06-07 16:08:47 +00:00
|
|
|
return "%s for %s" % (self.quantity, self.price)
|
2008-09-01 19:08:08 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (('price', 'quantity'),)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2014-03-14 14:18:08 +00:00
|
|
|
class Triple(models.Model):
|
|
|
|
left = models.IntegerField()
|
|
|
|
middle = models.IntegerField()
|
|
|
|
right = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (('left', 'middle'), ('middle', 'right'))
|
|
|
|
|
|
|
|
|
2008-08-31 20:11:11 +00:00
|
|
|
class ArticleStatus(models.Model):
|
|
|
|
status = models.CharField(max_length=2, choices=ARTICLE_STATUS_CHAR, blank=True, null=True)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2008-09-01 22:43:38 +00:00
|
|
|
class Inventory(models.Model):
|
2011-03-04 01:27:14 +00:00
|
|
|
barcode = models.PositiveIntegerField(unique=True)
|
2015-07-22 14:43:21 +00:00
|
|
|
parent = models.ForeignKey('self', models.SET_NULL, to_field='barcode', blank=True, null=True)
|
2011-03-04 01:27:14 +00:00
|
|
|
name = models.CharField(blank=False, max_length=20)
|
2008-09-01 19:08:08 +00:00
|
|
|
|
2011-03-04 01:27:14 +00:00
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2011-03-04 01:27:14 +00:00
|
|
|
return self.name
|
2008-10-21 14:04:24 +00:00
|
|
|
|
2012-08-14 21:38:35 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return self.__str__()
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-06-10 10:22:40 +00:00
|
|
|
class Book(models.Model):
|
|
|
|
title = models.CharField(max_length=40)
|
2015-07-22 14:43:21 +00:00
|
|
|
author = models.ForeignKey(Writer, models.SET_NULL, blank=True, null=True)
|
2013-06-10 10:22:40 +00:00
|
|
|
special_id = models.IntegerField(blank=True, null=True, unique=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('title', 'author')
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-03-16 19:32:11 +00:00
|
|
|
class BookXtra(models.Model):
|
|
|
|
isbn = models.CharField(max_length=16, unique=True)
|
|
|
|
suffix1 = models.IntegerField(blank=True, default=0)
|
|
|
|
suffix2 = models.IntegerField(blank=True, default=0)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (('suffix1', 'suffix2'))
|
|
|
|
abstract = True
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-03-16 19:32:11 +00:00
|
|
|
class DerivedBook(Book, BookXtra):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2008-11-05 19:47:44 +00:00
|
|
|
class ExplicitPK(models.Model):
|
|
|
|
key = models.CharField(max_length=20, primary_key=True)
|
|
|
|
desc = models.CharField(max_length=20, blank=True, unique=True)
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2008-11-05 19:47:44 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = ('key', 'desc')
|
2009-03-15 05:05:26 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-11-05 19:47:44 +00:00
|
|
|
return self.key
|
2008-10-21 14:04:24 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2009-04-30 13:47:39 +00:00
|
|
|
class Post(models.Model):
|
|
|
|
title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
|
|
|
|
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
|
|
|
|
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
|
|
|
|
posted = models.DateField()
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2013-05-28 13:56:14 +00:00
|
|
|
return self.title
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-05-28 13:56:14 +00:00
|
|
|
class DateTimePost(models.Model):
|
|
|
|
title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
|
|
|
|
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
|
|
|
|
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
|
|
|
|
posted = models.DateTimeField(editable=False)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
2009-04-30 13:47:39 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-03-16 19:32:11 +00:00
|
|
|
class DerivedPost(Post):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2009-12-17 15:10:38 +00:00
|
|
|
class BigInt(models.Model):
|
|
|
|
biggie = models.BigIntegerField()
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2016-12-29 15:27:49 +00:00
|
|
|
return str(self.biggie)
|
2009-12-17 15:10:38 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-03-31 07:43:52 +00:00
|
|
|
class MarkupField(models.CharField):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs["max_length"] = 20
|
2017-01-21 13:13:44 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2010-03-31 07:43:52 +00:00
|
|
|
|
|
|
|
def formfield(self, **kwargs):
|
|
|
|
# don't allow this field to be used in form (real use-case might be
|
|
|
|
# that you know the markup will always be X, but it is among an app
|
|
|
|
# that allows the user to say it could be something else)
|
|
|
|
# regressed at r10062
|
|
|
|
return None
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-03-31 07:43:52 +00:00
|
|
|
class CustomFieldForExclusionModel(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
markup = MarkupField()
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2011-01-09 13:26:39 +00:00
|
|
|
class FlexibleDatePost(models.Model):
|
|
|
|
title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
|
|
|
|
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
|
|
|
|
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
|
2011-02-26 08:50:15 +00:00
|
|
|
posted = models.DateField(blank=True, null=True)
|
2012-12-19 18:12:08 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-12-19 18:12:08 +00:00
|
|
|
class Colour(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
def __iter__(self):
|
2017-02-24 01:06:01 +00:00
|
|
|
yield from range(5)
|
2012-12-19 18:12:08 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-12-19 18:12:08 +00:00
|
|
|
class ColourfulItem(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
colours = models.ManyToManyField(Colour)
|
2013-05-21 21:32:39 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-06-05 18:55:05 +00:00
|
|
|
class CustomErrorMessage(models.Model):
|
2016-04-08 02:04:45 +00:00
|
|
|
name1 = models.CharField(
|
|
|
|
max_length=50,
|
2013-06-05 18:55:05 +00:00
|
|
|
validators=[validators.validate_slug],
|
2016-04-08 02:04:45 +00:00
|
|
|
error_messages={'invalid': 'Model custom error message.'},
|
|
|
|
)
|
|
|
|
name2 = models.CharField(
|
|
|
|
max_length=50,
|
2013-06-05 18:55:05 +00:00
|
|
|
validators=[validators.validate_slug],
|
2016-04-08 02:04:45 +00:00
|
|
|
error_messages={'invalid': 'Model custom error message.'},
|
|
|
|
)
|
2013-08-05 19:26:51 +00:00
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
if self.name1 == 'FORBIDDEN_VALUE':
|
|
|
|
raise ValidationError({'name1': [ValidationError('Model.clean() error messages.')]})
|
2014-11-18 17:58:43 +00:00
|
|
|
elif self.name1 == 'FORBIDDEN_VALUE2':
|
|
|
|
raise ValidationError({'name1': 'Model.clean() error messages (simpler syntax).'})
|
2014-03-14 14:18:08 +00:00
|
|
|
elif self.name1 == 'GLOBAL_ERROR':
|
|
|
|
raise ValidationError("Global error message.")
|
2014-02-01 19:23:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def today_callable_dict():
|
|
|
|
return {"last_action__gte": datetime.datetime.today()}
|
|
|
|
|
|
|
|
|
|
|
|
def today_callable_q():
|
|
|
|
return models.Q(last_action__gte=datetime.datetime.today())
|
|
|
|
|
|
|
|
|
|
|
|
class Character(models.Model):
|
|
|
|
username = models.CharField(max_length=100)
|
|
|
|
last_action = models.DateTimeField()
|
|
|
|
|
|
|
|
|
|
|
|
class StumpJoke(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
most_recently_fooled = models.ForeignKey(
|
|
|
|
Character,
|
|
|
|
models.CASCADE,
|
|
|
|
limit_choices_to=today_callable_dict,
|
|
|
|
related_name="+",
|
|
|
|
)
|
2014-02-01 19:23:31 +00:00
|
|
|
has_fooled_today = models.ManyToManyField(Character, limit_choices_to=today_callable_q, related_name="+")
|
2014-06-03 03:36:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Model for #13776
|
|
|
|
class Student(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
character = models.ForeignKey(Character, models.CASCADE)
|
2014-06-03 03:36:59 +00:00
|
|
|
study = models.CharField(max_length=30)
|
2014-07-26 01:23:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Model for #639
|
|
|
|
class Photo(models.Model):
|
|
|
|
title = models.CharField(max_length=30)
|
|
|
|
image = models.FileField(storage=temp_storage, upload_to='tests')
|
|
|
|
|
|
|
|
# Support code for the tests; this keeps track of how many times save()
|
|
|
|
# gets called on each instance.
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2014-07-26 01:23:01 +00:00
|
|
|
self._savecount = 0
|
|
|
|
|
|
|
|
def save(self, force_insert=False, force_update=False):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().save(force_insert, force_update)
|
2014-07-26 01:23:01 +00:00
|
|
|
self._savecount += 1
|
2015-08-06 21:43:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UUIDPK(models.Model):
|
|
|
|
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
|
|
name = models.CharField(max_length=30)
|
2015-04-30 07:57:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Models for #24706
|
|
|
|
class StrictAssignmentFieldSpecific(models.Model):
|
|
|
|
title = models.CharField(max_length=30)
|
|
|
|
_should_error = False
|
|
|
|
|
|
|
|
def __setattr__(self, key, value):
|
|
|
|
if self._should_error is True:
|
|
|
|
raise ValidationError(message={key: "Cannot set attribute"}, code='invalid')
|
2017-01-21 13:13:44 +00:00
|
|
|
super().__setattr__(key, value)
|
2015-04-30 07:57:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StrictAssignmentAll(models.Model):
|
|
|
|
title = models.CharField(max_length=30)
|
|
|
|
_should_error = False
|
|
|
|
|
|
|
|
def __setattr__(self, key, value):
|
|
|
|
if self._should_error is True:
|
|
|
|
raise ValidationError(message="Cannot set attribute", code='invalid')
|
2017-01-21 13:13:44 +00:00
|
|
|
super().__setattr__(key, value)
|
2015-11-14 16:38:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
# A model with ForeignKey(blank=False, null=True)
|
|
|
|
class Award(models.Model):
|
|
|
|
name = models.CharField(max_length=30)
|
|
|
|
character = models.ForeignKey(Character, models.SET_NULL, blank=False, null=True)
|
2016-05-18 14:30:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NullableUniqueCharFieldModel(models.Model):
|
|
|
|
codename = models.CharField(max_length=50, blank=True, null=True, unique=True)
|