2006-12-15 13:46:11 +08:00
|
|
|
"""
|
2007-12-03 03:29:54 +08:00
|
|
|
XX. Generating HTML forms from models
|
|
|
|
|
2008-08-12 22:15:38 +08:00
|
|
|
This is mostly just a reworking of the ``form_for_model``/``form_for_instance``
|
|
|
|
tests to use ``ModelForm``. As such, the text may not make sense in all cases,
|
|
|
|
and the examples are probably a poor fit for the ``ModelForm`` syntax. In other
|
|
|
|
words, most of these tests should be rewritten.
|
2006-12-15 13:46:11 +08:00
|
|
|
"""
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
2006-12-15 13:46:11 +08:00
|
|
|
|
2011-11-14 03:05:02 +08:00
|
|
|
import os
|
2008-01-18 02:03:21 +08:00
|
|
|
import tempfile
|
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
from django.core.files.storage import FileSystemStorage
|
2011-10-14 02:04:12 +08:00
|
|
|
from django.db import models
|
2012-07-20 20:48:51 +08:00
|
|
|
from django.utils import six
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
|
2011-11-14 03:05:02 +08:00
|
|
|
temp_storage_dir = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
|
2008-10-11 06:13:16 +08:00
|
|
|
temp_storage = FileSystemStorage(temp_storage_dir)
|
2006-12-15 13:46:11 +08:00
|
|
|
|
2007-04-28 22:18:03 +08:00
|
|
|
ARTICLE_STATUS = (
|
|
|
|
(1, 'Draft'),
|
|
|
|
(2, 'Pending'),
|
|
|
|
(3, 'Live'),
|
|
|
|
)
|
|
|
|
|
2008-09-01 04:11:11 +08:00
|
|
|
ARTICLE_STATUS_CHAR = (
|
|
|
|
('d', 'Draft'),
|
|
|
|
('p', 'Pending'),
|
|
|
|
('l', 'Live'),
|
|
|
|
)
|
|
|
|
|
2006-12-15 13:46:11 +08:00
|
|
|
class Category(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=20)
|
2007-09-08 13:09:39 +08:00
|
|
|
slug = models.SlugField(max_length=20)
|
2007-08-05 13:14:46 +08:00
|
|
|
url = models.CharField('The URL', max_length=40)
|
2006-12-15 13:46:11 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-12-15 13:46:11 +08:00
|
|
|
return self.name
|
|
|
|
|
2006-12-27 13:23:21 +08:00
|
|
|
class Writer(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50, help_text='Use both first and last names.')
|
2006-12-27 13:23:21 +08:00
|
|
|
|
2011-03-04 08:27:22 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-12-27 13:23:21 +08:00
|
|
|
return self.name
|
|
|
|
|
2006-12-15 13:46:11 +08:00
|
|
|
class Article(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
headline = models.CharField(max_length=50)
|
2007-09-08 13:09:39 +08:00
|
|
|
slug = models.SlugField()
|
2006-12-30 08:12:02 +08:00
|
|
|
pub_date = models.DateField()
|
2007-02-20 10:59:16 +08:00
|
|
|
created = models.DateField(editable=False)
|
2006-12-27 13:23:21 +08:00
|
|
|
writer = models.ForeignKey(Writer)
|
2007-01-11 07:34:37 +08:00
|
|
|
article = models.TextField()
|
2006-12-28 10:34:53 +08:00
|
|
|
categories = models.ManyToManyField(Category, blank=True)
|
2008-09-02 03:20:03 +08:00
|
|
|
status = models.PositiveIntegerField(choices=ARTICLE_STATUS, blank=True, null=True)
|
2006-12-15 13:46:11 +08:00
|
|
|
|
2007-02-20 10:59:16 +08:00
|
|
|
def save(self):
|
|
|
|
import datetime
|
|
|
|
if not self.id:
|
|
|
|
self.created = datetime.date.today()
|
|
|
|
return super(Article, self).save()
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-12-15 13:46:11 +08:00
|
|
|
return self.headline
|
|
|
|
|
2008-08-23 03:27:26 +08:00
|
|
|
class ImprovedArticle(models.Model):
|
|
|
|
article = models.OneToOneField(Article)
|
|
|
|
|
|
|
|
class ImprovedArticleWithParentLink(models.Model):
|
|
|
|
article = models.OneToOneField(Article, parent_link=True)
|
|
|
|
|
2008-08-25 11:51:25 +08:00
|
|
|
class BetterWriter(Writer):
|
2010-01-05 11:56:19 +08:00
|
|
|
score = models.IntegerField()
|
2008-08-25 11:51:25 +08:00
|
|
|
|
2008-08-31 17:49:55 +08:00
|
|
|
class WriterProfile(models.Model):
|
|
|
|
writer = models.OneToOneField(Writer, primary_key=True)
|
|
|
|
age = models.PositiveIntegerField()
|
2008-09-02 06:43:38 +08:00
|
|
|
|
2008-08-31 17:49:55 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return "%s is %s" % (self.writer, self.age)
|
|
|
|
|
2008-09-02 06:15:35 +08:00
|
|
|
from django.contrib.localflavor.us.models import PhoneNumberField
|
2007-02-21 13:59:46 +08:00
|
|
|
class PhoneNumber(models.Model):
|
2008-09-02 06:15:35 +08:00
|
|
|
phone = PhoneNumberField()
|
2007-08-05 13:14:46 +08:00
|
|
|
description = models.CharField(max_length=20)
|
2007-02-21 13:59:46 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2007-02-21 13:59:46 +08:00
|
|
|
return self.phone
|
|
|
|
|
2008-01-18 02:03:21 +08:00
|
|
|
class TextFile(models.Model):
|
|
|
|
description = models.CharField(max_length=20)
|
2009-03-31 06:52:16 +08:00
|
|
|
file = models.FileField(storage=temp_storage, upload_to='tests', max_length=15)
|
2008-01-18 02:03:21 +08:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.description
|
2008-02-14 20:56:49 +08:00
|
|
|
|
2009-02-17 02:34:28 +08:00
|
|
|
try:
|
2010-02-15 02:28:28 +08:00
|
|
|
# If PIL is available, try testing ImageFields. Checking for the existence
|
|
|
|
# of Image is enough for CPython, but for PyPy, you need to check for the
|
|
|
|
# underlying modules If PIL is not available, ImageField tests are omitted.
|
|
|
|
# Try to import PIL in either of the two ways it can end up installed.
|
|
|
|
try:
|
|
|
|
from PIL import Image, _imaging
|
|
|
|
except ImportError:
|
|
|
|
import Image, _imaging
|
2010-03-17 03:32:11 +08:00
|
|
|
|
2009-02-17 02:34:28 +08:00
|
|
|
test_images = True
|
2009-03-15 13:05:26 +08:00
|
|
|
|
2009-02-17 02:34:28 +08:00
|
|
|
class ImageFile(models.Model):
|
|
|
|
def custom_upload_path(self, filename):
|
|
|
|
path = self.path or 'tests'
|
|
|
|
return '%s/%s' % (path, filename)
|
2009-03-15 13:05:26 +08:00
|
|
|
|
2009-02-17 02:34:28 +08:00
|
|
|
description = models.CharField(max_length=20)
|
2009-05-11 17:57:19 +08: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-17 02:34:28 +08:00
|
|
|
width = models.IntegerField(editable=False)
|
|
|
|
height = models.IntegerField(editable=False)
|
2009-05-11 17:57:19 +08:00
|
|
|
image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
|
|
|
|
width_field='width', height_field='height')
|
2009-02-17 02:34:28 +08:00
|
|
|
path = models.CharField(max_length=16, blank=True, default='')
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.description
|
2009-03-15 13:05:26 +08:00
|
|
|
|
2009-02-17 02:34:28 +08:00
|
|
|
class OptionalImageFile(models.Model):
|
|
|
|
def custom_upload_path(self, filename):
|
|
|
|
path = self.path or 'tests'
|
|
|
|
return '%s/%s' % (path, filename)
|
2009-03-15 13:05:26 +08:00
|
|
|
|
2009-02-17 02:34:28 +08:00
|
|
|
description = models.CharField(max_length=20)
|
|
|
|
image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
|
2009-03-15 13:05:26 +08:00
|
|
|
width_field='width', height_field='height',
|
2009-02-17 02:34:28 +08: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='')
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.description
|
|
|
|
except ImportError:
|
|
|
|
test_images = False
|
2009-03-15 13:05:26 +08:00
|
|
|
|
2008-08-29 04:58:10 +08:00
|
|
|
class CommaSeparatedInteger(models.Model):
|
|
|
|
field = models.CommaSeparatedIntegerField(max_length=20)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.field
|
|
|
|
|
2008-09-02 03:08:08 +08:00
|
|
|
class Product(models.Model):
|
|
|
|
slug = models.SlugField(unique=True)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.slug
|
|
|
|
|
|
|
|
class Price(models.Model):
|
|
|
|
price = models.DecimalField(max_digits=10, decimal_places=2)
|
|
|
|
quantity = models.PositiveIntegerField()
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s for %s" % (self.quantity, self.price)
|
2008-09-02 03:08:08 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = (('price', 'quantity'),)
|
|
|
|
|
2008-09-01 04:11:11 +08:00
|
|
|
class ArticleStatus(models.Model):
|
|
|
|
status = models.CharField(max_length=2, choices=ARTICLE_STATUS_CHAR, blank=True, null=True)
|
|
|
|
|
2008-09-02 06:43:38 +08:00
|
|
|
class Inventory(models.Model):
|
2011-03-04 09:27:14 +08:00
|
|
|
barcode = models.PositiveIntegerField(unique=True)
|
|
|
|
parent = models.ForeignKey('self', to_field='barcode', blank=True, null=True)
|
|
|
|
name = models.CharField(blank=False, max_length=20)
|
2008-09-02 03:08:08 +08:00
|
|
|
|
2011-03-04 09:27:14 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
2008-10-21 22:04:24 +08:00
|
|
|
|
|
|
|
class Book(models.Model):
|
|
|
|
title = models.CharField(max_length=40)
|
|
|
|
author = models.ForeignKey(Writer, blank=True, null=True)
|
|
|
|
special_id = models.IntegerField(blank=True, null=True, unique=True)
|
2009-03-15 13:05:26 +08:00
|
|
|
|
2008-10-21 22:04:24 +08:00
|
|
|
class Meta:
|
|
|
|
unique_together = ('title', 'author')
|
2009-03-15 13:05:26 +08:00
|
|
|
|
2010-03-17 03:32:11 +08: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
|
|
|
|
|
|
|
|
class DerivedBook(Book, BookXtra):
|
|
|
|
pass
|
|
|
|
|
2008-11-06 03:47:44 +08:00
|
|
|
class ExplicitPK(models.Model):
|
|
|
|
key = models.CharField(max_length=20, primary_key=True)
|
|
|
|
desc = models.CharField(max_length=20, blank=True, unique=True)
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('key', 'desc')
|
2009-03-15 13:05:26 +08:00
|
|
|
|
2008-11-06 03:47:44 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.key
|
2008-10-21 22:04:24 +08:00
|
|
|
|
2009-04-30 21:47:39 +08: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()
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
2010-03-17 03:32:11 +08:00
|
|
|
class DerivedPost(Post):
|
|
|
|
pass
|
|
|
|
|
2009-12-17 23:10:38 +08:00
|
|
|
class BigInt(models.Model):
|
|
|
|
biggie = models.BigIntegerField()
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2012-07-20 20:48:51 +08:00
|
|
|
return six.text_type(self.biggie)
|
2009-12-17 23:10:38 +08:00
|
|
|
|
2010-03-31 15:43:52 +08:00
|
|
|
class MarkupField(models.CharField):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs["max_length"] = 20
|
|
|
|
super(MarkupField, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
class CustomFieldForExclusionModel(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
markup = MarkupField()
|
|
|
|
|
2011-01-09 21:26:39 +08: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 16:50:15 +08:00
|
|
|
posted = models.DateField(blank=True, null=True)
|