Ticket #13774: rel_db_type_cleanup.diff
File rel_db_type_cleanup.diff, 3.9 KB (added by , 14 years ago) |
---|
-
django/db/models/fields/__init__.py
195 195 self.run_validators(value) 196 196 return value 197 197 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 198 205 def db_type(self, connection): 199 206 """ 200 207 Returns the database column data type for this field, for the provided … … 215 222 # mapped to one of the built-in Django field types. In this case, you 216 223 # can implement db_type() instead of get_internal_type() to specify 217 224 # 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) 223 226 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 224 233 def unique(self): 225 234 return self._unique or self.primary_key 226 235 unique = property(unique) … … 459 468 kwargs['blank'] = True 460 469 Field.__init__(self, *args, **kwargs) 461 470 471 def rel_db_type(self, connection): 472 return self._internal_to_db_type('IntegerField', connection) 473 462 474 def to_python(self, value): 463 475 if value is None: 464 476 return value … … 963 975 def get_internal_type(self): 964 976 return "PositiveIntegerField" 965 977 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 966 983 def formfield(self, **kwargs): 967 984 defaults = {'min_value': 0} 968 985 defaults.update(kwargs) … … 973 990 def get_internal_type(self): 974 991 return "PositiveSmallIntegerField" 975 992 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 976 998 def formfield(self, **kwargs): 977 999 defaults = {'min_value': 0} 978 1000 defaults.update(kwargs) -
django/db/models/fields/related.py
894 894 return super(ForeignKey, self).formfield(**defaults) 895 895 896 896 def db_type(self, connection): 897 # The database column type of a ForeignKey is the column type898 # of the field to which it points. An exception is if the ForeignKey899 # 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 only902 # thing we can do is making AutoField an IntegerField.903 897 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) 910 899 911 900 class OneToOneField(ForeignKey): 912 901 """