diff -r 9b24e08becc2 django/db/backends/creation.py
a
|
b
|
|
3 | 3 | |
4 | 4 | from django.conf import settings |
5 | 5 | from django.core.management import call_command |
| 6 | from django.utils.datastructures import DictWrapper |
6 | 7 | |
7 | 8 | # The prefix to put on the default database name when creating |
8 | 9 | # the test database. |
… |
… |
|
26 | 27 | shorten identifying names. |
27 | 28 | """ |
28 | 29 | return '%x' % (abs(hash(args)) % 4294967296L) # 2**32 |
| 30 | |
| 31 | def db_type(self, field): |
| 32 | return self._db_type(field, field.get_internal_type()) |
| 33 | |
| 34 | def related_db_type(self, field): |
| 35 | return self._db_type(field, field.get_related_internal_type()) |
| 36 | |
| 37 | def _db_type(self, field, internal_type): |
| 38 | data = DictWrapper(field.__dict__, self.connection.ops.quote_name, "qn_") |
| 39 | try: |
| 40 | return self.connection.creation.data_types[internal_type] % data |
| 41 | except KeyError: |
| 42 | return None |
29 | 43 | |
30 | 44 | def sql_create_model(self, model, style, known_models=set()): |
31 | 45 | """ |
diff -r 9b24e08becc2 django/db/models/fields/__init__.py
a
|
b
|
|
13 | 13 | from django.conf import settings |
14 | 14 | from django import forms |
15 | 15 | from django.core import exceptions, validators |
16 | | from django.utils.datastructures import DictWrapper |
17 | 16 | from django.utils.functional import curry |
18 | 17 | from django.utils.text import capfirst |
19 | 18 | from django.utils.translation import ugettext_lazy as _ |
… |
… |
|
215 | 214 | # mapped to one of the built-in Django field types. In this case, you |
216 | 215 | # can implement db_type() instead of get_internal_type() to specify |
217 | 216 | # 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 |
| 217 | return connection.creation.db_type(self) |
| 218 | |
| 219 | def related_db_type(self, connection): |
| 220 | # This is the db_type used by a ForeignKey. |
| 221 | return connection.creation.related_db_type(self) |
223 | 222 | |
224 | 223 | def unique(self): |
225 | 224 | return self._unique or self.primary_key |
… |
… |
|
252 | 251 | def get_internal_type(self): |
253 | 252 | return self.__class__.__name__ |
254 | 253 | |
| 254 | def get_related_internal_type(self): |
| 255 | return self.get_internal_type() |
| 256 | |
255 | 257 | def pre_save(self, model_instance, add): |
256 | 258 | "Returns field's value just before saving." |
257 | 259 | return getattr(model_instance, self.attname) |
… |
… |
|
462 | 464 | def get_internal_type(self): |
463 | 465 | return "AutoField" |
464 | 466 | |
| 467 | def get_related_internal_type(self): |
| 468 | return "RelatedAutoField" |
| 469 | |
| 470 | def related_db_type(self, connection): |
| 471 | db_type = super(AutoField, self).related_db_type(connection=connection) |
| 472 | if db_type is None: |
| 473 | return IntegerField().db_type(connection=connection) |
| 474 | return db_type |
| 475 | |
465 | 476 | def to_python(self, value): |
466 | 477 | if value is None: |
467 | 478 | return value |
… |
… |
|
979 | 990 | class PositiveIntegerField(IntegerField): |
980 | 991 | description = _("Integer") |
981 | 992 | |
| 993 | def related_db_type(self, connection): |
| 994 | if not connection.features.related_fields_match_type: |
| 995 | return IntegerField().related_db_type(connection=connection) |
| 996 | return super(PositiveIntegerField, self).related_db_type( |
| 997 | connection=connection) |
| 998 | |
982 | 999 | def get_internal_type(self): |
983 | 1000 | return "PositiveIntegerField" |
984 | 1001 | |
… |
… |
|
989 | 1006 | |
990 | 1007 | class PositiveSmallIntegerField(IntegerField): |
991 | 1008 | description = _("Integer") |
| 1009 | |
| 1010 | def related_db_type(self, connection): |
| 1011 | if not connection.features.related_fields_match_type: |
| 1012 | return IntegerField().related_db_type(connection=connection) |
| 1013 | return super(PositiveSmallIntegerField, self).related_db_type( |
| 1014 | connection=connection) |
| 1015 | |
992 | 1016 | def get_internal_type(self): |
993 | 1017 | return "PositiveSmallIntegerField" |
994 | 1018 | |
diff -r 9b24e08becc2 django/db/models/fields/related.py
a
|
b
|
|
1 | 1 | from django.conf import settings |
2 | | from django.db import connection, router, transaction |
| 2 | from django.db import connection, router, transaction, connections |
3 | 3 | from django.db.backends import util |
4 | 4 | from django.db.models import signals, get_model |
5 | 5 | from django.db.models.fields import (AutoField, Field, IntegerField, |
… |
… |
|
859 | 859 | return None |
860 | 860 | else: |
861 | 861 | return self.rel.get_related_field().get_db_prep_save(value, |
862 | | connection=connection) |
| 862 | connection=connections[router.db_for_read(self.rel.to)]) |
863 | 863 | |
864 | 864 | def value_to_string(self, obj): |
865 | 865 | if not obj: |
… |
… |
|
908 | 908 | # If the database needs similar types for key fields however, the only |
909 | 909 | # thing we can do is making AutoField an IntegerField. |
910 | 910 | rel_field = self.rel.get_related_field() |
911 | | if (isinstance(rel_field, AutoField) or |
912 | | (not connection.features.related_fields_match_type and |
913 | | isinstance(rel_field, (PositiveIntegerField, |
914 | | PositiveSmallIntegerField)))): |
915 | | return IntegerField().db_type(connection=connection) |
916 | | return rel_field.db_type(connection=connection) |
| 911 | return rel_field.related_db_type(connection=connections[router.db_for_read(rel_field.model)]) |
917 | 912 | |
918 | 913 | class OneToOneField(ForeignKey): |
919 | 914 | """ |