Ticket #231: max_length_cleanups_with_docs.diff

File max_length_cleanups_with_docs.diff, 13.0 KB (added by donspaulding, 7 years ago)

Cleaned up handling of max_length in model fields, form fields, and added documentation. Tests included.

  • django/db/models/fields/__init__.py

     
    686686
    687687class EmailField(CharField):
    688688    def __init__(self, *args, **kwargs):
    689         if 'max_length' not in kwargs:
    690             kwargs['max_length'] = 75
     689        kwargs['max_length'] = kwargs.get('max_length', 75)
    691690        CharField.__init__(self, *args, **kwargs)
    692691
    693692    def get_internal_type(self):
     
    707706class FileField(Field):
    708707    def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
    709708        self.upload_to = upload_to
     709        kwargs['max_length'] = kwargs.get('max_length', 100)       
    710710        Field.__init__(self, verbose_name, name, **kwargs)
    711711
    712712    def get_db_prep_save(self, value):
     
    808808class FilePathField(Field):
    809809    def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs):
    810810        self.path, self.match, self.recursive = path, match, recursive
     811        kwargs['max_length'] = kwargs.get('max_length', 100)
    811812        Field.__init__(self, verbose_name, name, **kwargs)
    812813
    813814    def get_manipulator_field_objs(self):
  • django/db/backends/ado_mssql/creation.py

     
    66    'DateField':         'smalldatetime',
    77    'DateTimeField':     'smalldatetime',
    88    'DecimalField':      'numeric(%(max_digits)s, %(decimal_places)s)',
    9     'FileField':         'varchar(100)',
    10     'FilePathField':     'varchar(100)',
     9    'FileField':         'varchar(%(max_length)s)',
     10    'FilePathField':     'varchar(%(max_length)s)',
    1111    'FloatField':        'double precision',
    12     'ImageField':        'varchar(100)',
     12    'ImageField':        'varchar(%(max_length)s)',
    1313    'IntegerField':      'int',
    1414    'IPAddressField':    'char(15)',
    1515    'NullBooleanField':  'bit',
  • django/db/backends/mysql_old/creation.py

     
    1010    'DateField':         'date',
    1111    'DateTimeField':     'datetime',
    1212    'DecimalField':      'numeric(%(max_digits)s, %(decimal_places)s)',
    13     'FileField':         'varchar(100)',
    14     'FilePathField':     'varchar(100)',
     13    'FileField':         'varchar(%(max_length)s)',
     14    'FilePathField':     'varchar(%(max_length)s)',
    1515    'FloatField':        'double precision',
    16     'ImageField':        'varchar(100)',
     16    'ImageField':        'varchar(%(max_length)s)',
    1717    'IntegerField':      'integer',
    1818    'IPAddressField':    'char(15)',
    1919    'NullBooleanField':  'bool',
  • django/db/backends/postgresql/creation.py

     
    1010    'DateField':         'date',
    1111    'DateTimeField':     'timestamp with time zone',
    1212    'DecimalField':      'numeric(%(max_digits)s, %(decimal_places)s)',
    13     'FileField':         'varchar(100)',
    14     'FilePathField':     'varchar(100)',
     13    'FileField':         'varchar(%(max_length)s)',
     14    'FilePathField':     'varchar(%(max_length)s)',
    1515    'FloatField':        'double precision',
    16     'ImageField':        'varchar(100)',
     16    'ImageField':        'varchar(%(max_length)s)',
    1717    'IntegerField':      'integer',
    1818    'IPAddressField':    'inet',
    1919    'NullBooleanField':  'boolean',
  • django/db/backends/sqlite3/creation.py

     
    99    'DateField':                    'date',
    1010    'DateTimeField':                'datetime',
    1111    'DecimalField':                 'decimal',
    12     'FileField':                    'varchar(100)',
    13     'FilePathField':                'varchar(100)',
     12    'FileField':                    'varchar(%(max_length)s)',
     13    'FilePathField':                'varchar(%(max_length)s)',
    1414    'FloatField':                   'real',
    15     'ImageField':                   'varchar(100)',
     15    'ImageField':                   'varchar(%(max_length)s)',
    1616    'IntegerField':                 'integer',
    1717    'IPAddressField':               'char(15)',
    1818    'NullBooleanField':             'bool',
  • django/db/backends/mysql/creation.py

     
    1010    'DateField':         'date',
    1111    'DateTimeField':     'datetime',
    1212    'DecimalField':      'numeric(%(max_digits)s, %(decimal_places)s)',
    13     'FileField':         'varchar(100)',
    14     'FilePathField':     'varchar(100)',
     13    'FileField':         'varchar(%(max_length)s)',
     14    'FilePathField':     'varchar(%(max_length)s)',
    1515    'FloatField':        'double precision',
    16     'ImageField':        'varchar(100)',
     16    'ImageField':        'varchar(%(max_length)s)',
    1717    'IntegerField':      'integer',
    1818    'IPAddressField':    'char(15)',
    1919    'NullBooleanField':  'bool',
  • django/db/backends/oracle/creation.py

     
    1313    'DateField':                    'DATE',
    1414    'DateTimeField':                'TIMESTAMP',
    1515    'DecimalField':                 'NUMBER(%(max_digits)s, %(decimal_places)s)',
    16     'FileField':                    'NVARCHAR2(100)',
    17     'FilePathField':                'NVARCHAR2(100)',
     16    'FileField':                    'NVARCHAR2(%(max_length)s)',
     17    'FilePathField':                'NVARCHAR2(%(max_length)s)',
    1818    'FloatField':                   'DOUBLE PRECISION',
    19     'ImageField':                   'NVARCHAR2(100)',
     19    'ImageField':                   'NVARCHAR2(%(max_length)s)',
    2020    'IntegerField':                 'NUMBER(11)',
    2121    'IPAddressField':               'VARCHAR2(15)',
    2222    'NullBooleanField':             'NUMBER(1) CHECK ((%(column)s IN (0,1)) OR (%(column)s IS NULL))',
     
    2828    'SmallIntegerField':            'NUMBER(11)',
    2929    'TextField':                    'NCLOB',
    3030    'TimeField':                    'TIMESTAMP',
    31     'URLField':                     'VARCHAR2(200)',
     31    'URLField':                     'VARCHAR2(%(max_length)s)',
    3232    'USStateField':                 'CHAR(2)',
    3333}
    3434
  • django/oldforms/__init__.py

     
    447447            self.field_name, self.rows, self.cols, escape(data))
    448448
    449449class HiddenField(FormField):
    450     def __init__(self, field_name, is_required=False, validator_list=None):
     450    def __init__(self, field_name, is_required=False, validator_list=None, max_length=None):
    451451        if validator_list is None: validator_list = []
    452452        self.field_name, self.is_required = field_name, is_required
    453453        self.validator_list = validator_list[:]
     
    674674####################
    675675
    676676class FileUploadField(FormField):
    677     def __init__(self, field_name, is_required=False, validator_list=None):
     677    def __init__(self, field_name, is_required=False, validator_list=None, max_length=None):
    678678        if validator_list is None: validator_list = []
    679679        self.field_name, self.is_required = field_name, is_required
    680680        self.validator_list = [self.isNonEmptyFile] + validator_list
     
    946946
    947947class FilePathField(SelectField):
    948948    "A SelectField whose choices are the files in a given directory."
    949     def __init__(self, field_name, path, match=None, recursive=False, is_required=False, validator_list=None):
     949    def __init__(self, field_name, path, match=None, recursive=False, is_required=False, validator_list=None, max_length=None):
    950950        import os
    951951        from django.db.models import BLANK_CHOICE_DASH
    952952        if match is not None:
  • tests/regressiontests/max_lengths/__init__.py

     
     1
  • tests/regressiontests/max_lengths/tests.py

     
     1from django.test import TestCase
     2from regressiontests.max_lengths.models import OldPerson, NewPerson
     3
     4class MaxLengths(TestCase):
     5       
     6    def verify_max_length(self, model,field,length):
     7        self.assertEquals(model._meta.get_field(field).max_length,length)
     8       
     9    def test_oldp(self):
     10        self.verify_max_length(OldPerson, 'email', 75)
     11        self.verify_max_length(OldPerson, 'vcard', 100)
     12        self.verify_max_length(OldPerson, 'your_mom', 100)
     13        self.verify_max_length(OldPerson, 'homepage', 200)
     14        self.verify_max_length(OldPerson, 'avatar', 100)
     15
     16    def test_newp(self):
     17        self.verify_max_length(NewPerson, 'email', 384)
     18        self.verify_max_length(NewPerson, 'vcard', 1024)
     19        self.verify_max_length(NewPerson, 'your_mom', 512)
     20        self.verify_max_length(NewPerson, 'homepage', 256)
     21        self.verify_max_length(NewPerson, 'avatar', 512)
  • tests/regressiontests/max_lengths/models.py

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: tests/regressiontests/max_lengths/__init__.pyc
    ___________________________________________________________________
    Name: svn:mime-type
       + application/octet-stream
    
     
     1from django.db import models
     2
     3class OldPerson(models.Model):
     4    email = models.EmailField()
     5    vcard = models.FileField(upload_to='/tmp')
     6    your_mom = models.ImageField(upload_to='/tmp')
     7    homepage = models.URLField()
     8    avatar = models.FilePathField()
     9
     10class NewPerson(models.Model):
     11    email = models.EmailField(max_length=384)
     12    vcard = models.FileField(upload_to='/tmp', max_length=1024)
     13    your_mom = models.ImageField(upload_to='/tmp', max_length=512)
     14    homepage = models.URLField(max_length=256)
     15    avatar = models.FilePathField(max_length=512)
  • docs/model-api.txt

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: tests/regressiontests/max_lengths/tests.pyc
    ___________________________________________________________________
    Name: svn:mime-type
       + application/octet-stream
    
    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: tests/regressiontests/max_lengths/models.pyc
    ___________________________________________________________________
    Name: svn:mime-type
       + application/octet-stream
    
     
    293293
    294294.. _`strftime formatting`: http://docs.python.org/lib/module-time.html#l2h-1941
    295295
     296In Django 0.96, this doesn't accept ``max_length``; its ``max_length`` is
     297automatically set to 100. In the Django development version, ``max_length`` is
     298set to 100 by default, but you can specify it to override default behavior.
     299
    296300``FilePathField``
    297301~~~~~~~~~~~~~~~~~
    298302
     
    330334because the ``match`` applies to the base filename (``foo.gif`` and
    331335``bar.gif``).
    332336
     337In Django 0.96, this doesn't accept ``max_length``; its ``max_length`` is
     338automatically set to 100. In the Django development version, ``max_length`` is
     339set to 100 by default, but you can specify it to override default behavior.
     340
    333341``FloatField``
    334342~~~~~~~~~~~~~~
    335343
     
    361369.. _Python Imaging Library: http://www.pythonware.com/products/pil/
    362370.. _elsewhere: ../db-api/#get-foo-height-and-get-foo-width
    363371
     372In Django 0.96, this doesn't accept ``max_length``; its ``max_length`` is
     373automatically set to 100. In the Django development version, ``max_length`` is
     374set to 100 by default, but you can specify it to override default behavior.
     375
    364376``IntegerField``
    365377~~~~~~~~~~~~~~~~
    366378
Back to Top