Fixed #231: all fields that should take max_length now do. Thanks, Don Spaulding.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6378 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
d022e2581d
commit
980fa8b827
1
AUTHORS
1
AUTHORS
|
@ -276,6 +276,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
sopel
|
sopel
|
||||||
Leo Soto <leo.soto@gmail.com>
|
Leo Soto <leo.soto@gmail.com>
|
||||||
Wiliam Alves de Souza <wiliamsouza83@gmail.com>
|
Wiliam Alves de Souza <wiliamsouza83@gmail.com>
|
||||||
|
Don Spaulding <donspauldingii@gmail.com>
|
||||||
Bjørn Stabell <bjorn@exoweb.net>
|
Bjørn Stabell <bjorn@exoweb.net>
|
||||||
Georgi Stanojevski <glisha@gmail.com>
|
Georgi Stanojevski <glisha@gmail.com>
|
||||||
Vasiliy Stavenko <stavenko@gmail.com>
|
Vasiliy Stavenko <stavenko@gmail.com>
|
||||||
|
|
|
@ -6,10 +6,10 @@ DATA_TYPES = {
|
||||||
'DateField': 'smalldatetime',
|
'DateField': 'smalldatetime',
|
||||||
'DateTimeField': 'smalldatetime',
|
'DateTimeField': 'smalldatetime',
|
||||||
'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
|
'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
|
||||||
'FileField': 'varchar(100)',
|
'FileField': 'varchar(%(max_length)s)',
|
||||||
'FilePathField': 'varchar(100)',
|
'FilePathField': 'varchar(%(max_length)s)',
|
||||||
'FloatField': 'double precision',
|
'FloatField': 'double precision',
|
||||||
'ImageField': 'varchar(100)',
|
'ImageField': 'varchar(%(max_length)s)',
|
||||||
'IntegerField': 'int',
|
'IntegerField': 'int',
|
||||||
'IPAddressField': 'char(15)',
|
'IPAddressField': 'char(15)',
|
||||||
'NullBooleanField': 'bit',
|
'NullBooleanField': 'bit',
|
||||||
|
|
|
@ -10,10 +10,10 @@ DATA_TYPES = {
|
||||||
'DateField': 'date',
|
'DateField': 'date',
|
||||||
'DateTimeField': 'datetime',
|
'DateTimeField': 'datetime',
|
||||||
'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
|
'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
|
||||||
'FileField': 'varchar(100)',
|
'FileField': 'varchar(%(max_length)s)',
|
||||||
'FilePathField': 'varchar(100)',
|
'FilePathField': 'varchar(%(max_length)s)',
|
||||||
'FloatField': 'double precision',
|
'FloatField': 'double precision',
|
||||||
'ImageField': 'varchar(100)',
|
'ImageField': 'varchar(%(max_length)s)',
|
||||||
'IntegerField': 'integer',
|
'IntegerField': 'integer',
|
||||||
'IPAddressField': 'char(15)',
|
'IPAddressField': 'char(15)',
|
||||||
'NullBooleanField': 'bool',
|
'NullBooleanField': 'bool',
|
||||||
|
|
|
@ -10,10 +10,10 @@ DATA_TYPES = {
|
||||||
'DateField': 'date',
|
'DateField': 'date',
|
||||||
'DateTimeField': 'datetime',
|
'DateTimeField': 'datetime',
|
||||||
'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
|
'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
|
||||||
'FileField': 'varchar(100)',
|
'FileField': 'varchar(%(max_length)s)',
|
||||||
'FilePathField': 'varchar(100)',
|
'FilePathField': 'varchar(%(max_length)s)',
|
||||||
'FloatField': 'double precision',
|
'FloatField': 'double precision',
|
||||||
'ImageField': 'varchar(100)',
|
'ImageField': 'varchar(%(max_length)s)',
|
||||||
'IntegerField': 'integer',
|
'IntegerField': 'integer',
|
||||||
'IPAddressField': 'char(15)',
|
'IPAddressField': 'char(15)',
|
||||||
'NullBooleanField': 'bool',
|
'NullBooleanField': 'bool',
|
||||||
|
|
|
@ -13,10 +13,10 @@ DATA_TYPES = {
|
||||||
'DateField': 'DATE',
|
'DateField': 'DATE',
|
||||||
'DateTimeField': 'TIMESTAMP',
|
'DateTimeField': 'TIMESTAMP',
|
||||||
'DecimalField': 'NUMBER(%(max_digits)s, %(decimal_places)s)',
|
'DecimalField': 'NUMBER(%(max_digits)s, %(decimal_places)s)',
|
||||||
'FileField': 'NVARCHAR2(100)',
|
'FileField': 'NVARCHAR2(%(max_length)s)',
|
||||||
'FilePathField': 'NVARCHAR2(100)',
|
'FilePathField': 'NVARCHAR2(%(max_length)s)',
|
||||||
'FloatField': 'DOUBLE PRECISION',
|
'FloatField': 'DOUBLE PRECISION',
|
||||||
'ImageField': 'NVARCHAR2(100)',
|
'ImageField': 'NVARCHAR2(%(max_length)s)',
|
||||||
'IntegerField': 'NUMBER(11)',
|
'IntegerField': 'NUMBER(11)',
|
||||||
'IPAddressField': 'VARCHAR2(15)',
|
'IPAddressField': 'VARCHAR2(15)',
|
||||||
'NullBooleanField': 'NUMBER(1) CHECK ((%(column)s IN (0,1)) OR (%(column)s IS NULL))',
|
'NullBooleanField': 'NUMBER(1) CHECK ((%(column)s IN (0,1)) OR (%(column)s IS NULL))',
|
||||||
|
@ -28,7 +28,7 @@ DATA_TYPES = {
|
||||||
'SmallIntegerField': 'NUMBER(11)',
|
'SmallIntegerField': 'NUMBER(11)',
|
||||||
'TextField': 'NCLOB',
|
'TextField': 'NCLOB',
|
||||||
'TimeField': 'TIMESTAMP',
|
'TimeField': 'TIMESTAMP',
|
||||||
'URLField': 'VARCHAR2(200)',
|
'URLField': 'VARCHAR2(%(max_length)s)',
|
||||||
'USStateField': 'CHAR(2)',
|
'USStateField': 'CHAR(2)',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,10 +10,10 @@ DATA_TYPES = {
|
||||||
'DateField': 'date',
|
'DateField': 'date',
|
||||||
'DateTimeField': 'timestamp with time zone',
|
'DateTimeField': 'timestamp with time zone',
|
||||||
'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
|
'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
|
||||||
'FileField': 'varchar(100)',
|
'FileField': 'varchar(%(max_length)s)',
|
||||||
'FilePathField': 'varchar(100)',
|
'FilePathField': 'varchar(%(max_length)s)',
|
||||||
'FloatField': 'double precision',
|
'FloatField': 'double precision',
|
||||||
'ImageField': 'varchar(100)',
|
'ImageField': 'varchar(%(max_length)s)',
|
||||||
'IntegerField': 'integer',
|
'IntegerField': 'integer',
|
||||||
'IPAddressField': 'inet',
|
'IPAddressField': 'inet',
|
||||||
'NullBooleanField': 'boolean',
|
'NullBooleanField': 'boolean',
|
||||||
|
|
|
@ -9,10 +9,10 @@ DATA_TYPES = {
|
||||||
'DateField': 'date',
|
'DateField': 'date',
|
||||||
'DateTimeField': 'datetime',
|
'DateTimeField': 'datetime',
|
||||||
'DecimalField': 'decimal',
|
'DecimalField': 'decimal',
|
||||||
'FileField': 'varchar(100)',
|
'FileField': 'varchar(%(max_length)s)',
|
||||||
'FilePathField': 'varchar(100)',
|
'FilePathField': 'varchar(%(max_length)s)',
|
||||||
'FloatField': 'real',
|
'FloatField': 'real',
|
||||||
'ImageField': 'varchar(100)',
|
'ImageField': 'varchar(%(max_length)s)',
|
||||||
'IntegerField': 'integer',
|
'IntegerField': 'integer',
|
||||||
'IPAddressField': 'char(15)',
|
'IPAddressField': 'char(15)',
|
||||||
'NullBooleanField': 'bool',
|
'NullBooleanField': 'bool',
|
||||||
|
|
|
@ -686,8 +686,7 @@ class DecimalField(Field):
|
||||||
|
|
||||||
class EmailField(CharField):
|
class EmailField(CharField):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
if 'max_length' not in kwargs:
|
kwargs['max_length'] = kwargs.get('max_length', 75)
|
||||||
kwargs['max_length'] = 75
|
|
||||||
CharField.__init__(self, *args, **kwargs)
|
CharField.__init__(self, *args, **kwargs)
|
||||||
|
|
||||||
def get_internal_type(self):
|
def get_internal_type(self):
|
||||||
|
@ -707,6 +706,7 @@ class EmailField(CharField):
|
||||||
class FileField(Field):
|
class FileField(Field):
|
||||||
def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
|
def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
|
||||||
self.upload_to = upload_to
|
self.upload_to = upload_to
|
||||||
|
kwargs['max_length'] = kwargs.get('max_length', 100)
|
||||||
Field.__init__(self, verbose_name, name, **kwargs)
|
Field.__init__(self, verbose_name, name, **kwargs)
|
||||||
|
|
||||||
def get_db_prep_save(self, value):
|
def get_db_prep_save(self, value):
|
||||||
|
@ -808,6 +808,7 @@ class FileField(Field):
|
||||||
class FilePathField(Field):
|
class FilePathField(Field):
|
||||||
def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
|
def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
|
||||||
self.path, self.match, self.recursive = path, match, recursive
|
self.path, self.match, self.recursive = path, match, recursive
|
||||||
|
kwargs['max_length'] = kwargs.get('max_length', 100)
|
||||||
Field.__init__(self, verbose_name, name, **kwargs)
|
Field.__init__(self, verbose_name, name, **kwargs)
|
||||||
|
|
||||||
def get_manipulator_field_objs(self):
|
def get_manipulator_field_objs(self):
|
||||||
|
|
|
@ -447,7 +447,7 @@ class LargeTextField(TextField):
|
||||||
self.field_name, self.rows, self.cols, escape(data))
|
self.field_name, self.rows, self.cols, escape(data))
|
||||||
|
|
||||||
class HiddenField(FormField):
|
class HiddenField(FormField):
|
||||||
def __init__(self, field_name, is_required=False, validator_list=None):
|
def __init__(self, field_name, is_required=False, validator_list=None, max_length=None):
|
||||||
if validator_list is None: validator_list = []
|
if validator_list is None: validator_list = []
|
||||||
self.field_name, self.is_required = field_name, is_required
|
self.field_name, self.is_required = field_name, is_required
|
||||||
self.validator_list = validator_list[:]
|
self.validator_list = validator_list[:]
|
||||||
|
@ -674,7 +674,7 @@ class CheckboxSelectMultipleField(SelectMultipleField):
|
||||||
####################
|
####################
|
||||||
|
|
||||||
class FileUploadField(FormField):
|
class FileUploadField(FormField):
|
||||||
def __init__(self, field_name, is_required=False, validator_list=None):
|
def __init__(self, field_name, is_required=False, validator_list=None, max_length=None):
|
||||||
if validator_list is None: validator_list = []
|
if validator_list is None: validator_list = []
|
||||||
self.field_name, self.is_required = field_name, is_required
|
self.field_name, self.is_required = field_name, is_required
|
||||||
self.validator_list = [self.isNonEmptyFile] + validator_list
|
self.validator_list = [self.isNonEmptyFile] + validator_list
|
||||||
|
@ -946,7 +946,7 @@ class IPAddressField(TextField):
|
||||||
|
|
||||||
class FilePathField(SelectField):
|
class FilePathField(SelectField):
|
||||||
"A SelectField whose choices are the files in a given directory."
|
"A SelectField whose choices are the files in a given directory."
|
||||||
def __init__(self, field_name, path, match=None, recursive=False, is_required=False, validator_list=None):
|
def __init__(self, field_name, path, match=None, recursive=False, is_required=False, validator_list=None, max_length=None):
|
||||||
import os
|
import os
|
||||||
from django.db.models import BLANK_CHOICE_DASH
|
from django.db.models import BLANK_CHOICE_DASH
|
||||||
if match is not None:
|
if match is not None:
|
||||||
|
|
|
@ -293,6 +293,12 @@ visiting its URL on your site. Don't allow that.
|
||||||
|
|
||||||
.. _`strftime formatting`: http://docs.python.org/lib/module-time.html#l2h-1941
|
.. _`strftime formatting`: http://docs.python.org/lib/module-time.html#l2h-1941
|
||||||
|
|
||||||
|
**New in development version:**
|
||||||
|
|
||||||
|
By default, file fields are created as ``varchar(100)`` database fields. Like
|
||||||
|
other fields, you can change the maximum length using the ``max_length``
|
||||||
|
argument.
|
||||||
|
|
||||||
``FilePathField``
|
``FilePathField``
|
||||||
~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@ -330,6 +336,12 @@ not the full path. So, this example::
|
||||||
because the ``match`` applies to the base filename (``foo.gif`` and
|
because the ``match`` applies to the base filename (``foo.gif`` and
|
||||||
``bar.gif``).
|
``bar.gif``).
|
||||||
|
|
||||||
|
**New in development version:**
|
||||||
|
|
||||||
|
By default, file fields are created as ``varchar(100)`` database fields. Like
|
||||||
|
other fields, you can change the maximum length using the ``max_length``
|
||||||
|
argument.
|
||||||
|
|
||||||
``FloatField``
|
``FloatField``
|
||||||
~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@ -361,6 +373,12 @@ Requires the `Python Imaging Library`_.
|
||||||
.. _Python Imaging Library: http://www.pythonware.com/products/pil/
|
.. _Python Imaging Library: http://www.pythonware.com/products/pil/
|
||||||
.. _elsewhere: ../db-api/#get-foo-height-and-get-foo-width
|
.. _elsewhere: ../db-api/#get-foo-height-and-get-foo-width
|
||||||
|
|
||||||
|
**New in development version:**
|
||||||
|
|
||||||
|
By default, file fields are created as ``varchar(100)`` database fields. Like
|
||||||
|
other fields, you can change the maximum length using the ``max_length``
|
||||||
|
argument.
|
||||||
|
|
||||||
``IntegerField``
|
``IntegerField``
|
||||||
~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
class PersonWithDefaultMaxLengths(models.Model):
|
||||||
|
email = models.EmailField()
|
||||||
|
vcard = models.FileField(upload_to='/tmp')
|
||||||
|
homepage = models.URLField()
|
||||||
|
avatar = models.FilePathField()
|
||||||
|
|
||||||
|
class PersonWithCustomMaxLengths(models.Model):
|
||||||
|
email = models.EmailField(max_length=384)
|
||||||
|
vcard = models.FileField(upload_to='/tmp', max_length=1024)
|
||||||
|
homepage = models.URLField(max_length=256)
|
||||||
|
avatar = models.FilePathField(max_length=512)
|
|
@ -0,0 +1,36 @@
|
||||||
|
from unittest import TestCase
|
||||||
|
from django.db import DatabaseError
|
||||||
|
from regressiontests.max_lengths.models import PersonWithDefaultMaxLengths, PersonWithCustomMaxLengths
|
||||||
|
|
||||||
|
class MaxLengthArgumentsTests(TestCase):
|
||||||
|
|
||||||
|
def verify_max_length(self, model,field,length):
|
||||||
|
self.assertEquals(model._meta.get_field(field).max_length,length)
|
||||||
|
|
||||||
|
def test_default_max_lengths(self):
|
||||||
|
self.verify_max_length(PersonWithDefaultMaxLengths, 'email', 75)
|
||||||
|
self.verify_max_length(PersonWithDefaultMaxLengths, 'vcard', 100)
|
||||||
|
self.verify_max_length(PersonWithDefaultMaxLengths, 'homepage', 200)
|
||||||
|
self.verify_max_length(PersonWithDefaultMaxLengths, 'avatar', 100)
|
||||||
|
|
||||||
|
def test_custom_maxlengths(self):
|
||||||
|
self.verify_max_length(PersonWithCustomMaxLengths, 'email', 384)
|
||||||
|
self.verify_max_length(PersonWithCustomMaxLengths, 'vcard', 1024)
|
||||||
|
self.verify_max_length(PersonWithCustomMaxLengths, 'homepage', 256)
|
||||||
|
self.verify_max_length(PersonWithCustomMaxLengths, 'avatar', 512)
|
||||||
|
|
||||||
|
class MaxLengthORMTests(TestCase):
|
||||||
|
|
||||||
|
def test_custom_max_lengths(self):
|
||||||
|
args = {
|
||||||
|
"email": "someone@example.com",
|
||||||
|
"vcard": "vcard",
|
||||||
|
"homepage": "http://example.com/",
|
||||||
|
"avatar": "me.jpg"
|
||||||
|
}
|
||||||
|
|
||||||
|
for field in ("email", "vcard", "homepage", "avatar"):
|
||||||
|
new_args = args.copy()
|
||||||
|
new_args[field] = "X" * 250 # a value longer than any of the default fields could hold.
|
||||||
|
p = PersonWithCustomMaxLengths.objects.create(**new_args)
|
||||||
|
self.assertEqual(getattr(p, field), ("X" * 250))
|
Loading…
Reference in New Issue