Ticket #7789: 7789_fields_2.diff

File 7789_fields_2.diff, 2.2 KB (added by arne, 16 years ago)

Alternate aproach

  • db/models/fields/__init__.py

     
    149149        # mapped to one of the built-in Django field types. In this case, you
    150150        # can implement db_type() instead of get_internal_type() to specify
    151151        # exactly which wacky database column type you want to use.
    152         data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")
    153152        try:
     153            data = get_creation_module().backend_parameters(self, self.__dict__)
     154        except AttributeError:
     155            # No backend_parameters provided in backend
     156            data = self.__dict__
     157        data = DictWrapper(data, connection.ops.quote_name, "qn_")
     158        try:
    154159            return get_creation_module().DATA_TYPES[self.get_internal_type()] % data
    155160        except KeyError:
    156161            return None
  • db/backends/mysql/creation.py

     
    55DATA_TYPES = {
    66    'AutoField':         'integer AUTO_INCREMENT',
    77    'BooleanField':      'bool',
    8     'CharField':         'varchar(%(max_length)s)',
     8    'CharField':         'varchar(%(max_length)s) %(binary)s',
    99    'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
    1010    'DateField':         'date',
    1111    'DateTimeField':     'datetime',
     
    2020    'PhoneNumberField':  'varchar(20)',
    2121    'PositiveIntegerField': 'integer UNSIGNED',
    2222    'PositiveSmallIntegerField': 'smallint UNSIGNED',
    23     'SlugField':         'varchar(%(max_length)s)',
     23    'SlugField':         'varchar(%(max_length)s) BINARY',
    2424    'SmallIntegerField': 'smallint',
    2525    'TextField':         'longtext',
    2626    'TimeField':         'time',
    2727    'USStateField':      'varchar(2)',
    2828}
     29
     30
     31def backend_parameters(field, field_dict):
     32    """
     33    For MySQL a unique CharField has to be created as a BINARY column as
     34    database level.
     35    """
     36    if field.unique and field.get_internal_type() == 'CharField':
     37        return dict(field_dict, binary='BINARY')
     38    return dict(field_dict, binary='')
Back to Top