Ticket #2596: field_form_parameters.patch

File field_form_parameters.patch, 2.5 KB (added by norvegh, 18 years ago)

patch for the extra field parameters

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

    old new  
    372372        return [forms.CheckboxField]
    373373
    374374class CharField(Field):
     375    def __init__(self, *args, **kvargs):
     376        self.form_type = kvargs.pop( 'form_type', 'input' )
     377        self.form_length = kvargs.pop( 'form_length', None )
     378        self.form_rows = kvargs.pop( 'form_rows', None )
     379        self.form_cols = kvargs.pop( 'form_cols', None )
     380        if self.form_type == 'input':
     381            if self.form_rows: raise NameError('form_rows parameter is not allowed for form_type "input"')
     382            if self.form_cols: raise NameError('form_cols parameter is not allowed for form_type "input"')
     383        elif self.form_type == 'textarea':
     384            if self.form_length: raise NameError('form_length parameter is not allowed for form_type "textarea"')
     385        else:
     386            raise NameError('invalid form_type argument: ' + self.form_type )
     387        Field.__init__( self, *args, **kvargs )
     388
     389    def prepare_field_objs_and_params(self, manipulator, name_prefix):
     390        field_objs, params = Field.prepare_field_objs_and_params(self, manipulator, name_prefix)
     391        if self.form_length:
     392            params['length'] = self.form_length
     393        if self.form_rows:
     394            params['rows'] = self.form_rows
     395        if self.form_cols:
     396            params['cols'] = self.form_cols
     397        return (field_objs, params)
     398
    375399    def get_manipulator_field_objs(self):
    376         return [forms.TextField]
     400        if self.form_type == "textarea":
     401            return [forms.LargeTextField]
     402        else:
     403            return [forms.TextField]
    377404
    378405    def to_python(self, value):
    379406        if isinstance(value, basestring):
     
    711808        return [forms.SmallIntegerField]
    712809
    713810class TextField(Field):
     811    def __init__(self, *args, **kvargs):
     812        self.form_rows = kvargs.pop( 'form_rows', None )
     813        self.form_cols = kvargs.pop( 'form_cols', None )
     814        Field.__init__(self, *args, **kvargs)
     815
     816    def prepare_field_objs_and_params(self, manipulator, name_prefix):
     817        field_objs, params = Field.prepare_field_objs_and_params(self, manipulator, name_prefix)
     818        if self.form_rows:
     819            params['rows'] = self.form_rows
     820        if self.form_cols:
     821            params['cols'] = self.form_cols
     822        return (field_objs, params)
     823
    714824    def get_manipulator_field_objs(self):
    715825        return [forms.LargeTextField]
    716826
Back to Top