Ticket #13774: rel_db_type_cleanup.diff

File rel_db_type_cleanup.diff, 3.9 KB (added by Alexander Schepanovski, 14 years ago)

rel_db_type for all fields, custom for AutoField, PositiveIntegerField and PositiveSmallIntegerField

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

     
    195195        self.run_validators(value)
    196196        return value
    197197
     198    def _internal_to_db_type(self, internal_type, connection):
     199        data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")
     200        try:
     201            return connection.creation.data_types[internal_type] % data
     202        except KeyError:
     203            return None
     204
    198205    def db_type(self, connection):
    199206        """
    200207        Returns the database column data type for this field, for the provided
     
    215222        # mapped to one of the built-in Django field types. In this case, you
    216223        # can implement db_type() instead of get_internal_type() to specify
    217224        # exactly which wacky database column type you want to use.
    218         data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")
    219         try:
    220             return connection.creation.data_types[self.get_internal_type()] % data
    221         except KeyError:
    222             return None
     225        return self._internal_to_db_type(self.get_internal_type(), connection)
    223226
     227    def rel_db_type(self, connection):
     228        """
     229        Returns the database column data type for related field referencing to this.
     230        """
     231        return self.db_type(connection)
     232
    224233    def unique(self):
    225234        return self._unique or self.primary_key
    226235    unique = property(unique)
     
    459468        kwargs['blank'] = True
    460469        Field.__init__(self, *args, **kwargs)
    461470
     471    def rel_db_type(self, connection):
     472        return self._internal_to_db_type('IntegerField', connection)
     473
    462474    def to_python(self, value):
    463475        if value is None:
    464476            return value
     
    963975    def get_internal_type(self):
    964976        return "PositiveIntegerField"
    965977
     978    def rel_db_type(self, connection):
     979        if connection.features.related_fields_match_type:
     980            return self.db_type(connection)
     981        return self._internal_to_db_type('IntegerField', connection)
     982
    966983    def formfield(self, **kwargs):
    967984        defaults = {'min_value': 0}
    968985        defaults.update(kwargs)
     
    973990    def get_internal_type(self):
    974991        return "PositiveSmallIntegerField"
    975992
     993    def rel_db_type(self, connection):
     994        if connection.features.related_fields_match_type:
     995            return self.db_type(connection)
     996        return self._internal_to_db_type('IntegerField', connection)
     997
    976998    def formfield(self, **kwargs):
    977999        defaults = {'min_value': 0}
    9781000        defaults.update(kwargs)
  • django/db/models/fields/related.py

     
    894894        return super(ForeignKey, self).formfield(**defaults)
    895895
    896896    def db_type(self, connection):
    897         # The database column type of a ForeignKey is the column type
    898         # of the field to which it points. An exception is if the ForeignKey
    899         # points to an AutoField/PositiveIntegerField/PositiveSmallIntegerField,
    900         # in which case the column type is simply that of an IntegerField.
    901         # If the database needs similar types for key fields however, the only
    902         # thing we can do is making AutoField an IntegerField.
    903897        rel_field = self.rel.get_related_field()
    904         if (isinstance(rel_field, AutoField) or
    905                 (not connection.features.related_fields_match_type and
    906                 isinstance(rel_field, (PositiveIntegerField,
    907                                        PositiveSmallIntegerField)))):
    908             return IntegerField().db_type(connection=connection)
    909         return rel_field.db_type(connection=connection)
     898        return rel_field.rel_db_type(connection=connection)
    910899
    911900class OneToOneField(ForeignKey):
    912901    """
Back to Top