commit 445d4298430683366f83fc69d59109023a60edd7
Author: Jonas Haag <jonas@lophus.org>
Date: Wed Sep 28 21:48:06 2011 +0200
Don't hardcode AutoField type to int
diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index f2bde84..588646f 100644
a
|
b
|
class BaseDatabaseOperations(object):
|
721 | 721 | # need not necessarily be implemented using "LIKE" in the backend. |
722 | 722 | prep_for_iexact_query = prep_for_like_query |
723 | 723 | |
| 724 | def value_to_db_auto(self, value): |
| 725 | """ |
| 726 | Transform a value to an object compatible with the auto field required |
| 727 | by the backend driver for auto columns. |
| 728 | """ |
| 729 | if value is None: |
| 730 | return None |
| 731 | return int(value) |
| 732 | |
724 | 733 | def value_to_db_date(self, value): |
725 | 734 | """ |
726 | 735 | Transform a date value to an object compatible with what is expected |
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index bdb8233..a02a8ab 100644
a
|
b
|
class AutoField(Field):
|
529 | 529 | return db_type |
530 | 530 | |
531 | 531 | def to_python(self, value): |
532 | | if value is None: |
533 | | return value |
534 | | try: |
535 | | return int(value) |
536 | | except (TypeError, ValueError): |
| 532 | if not (value is None or isinstance(value, (basestring, int, long))): |
537 | 533 | msg = self.error_messages['invalid'] % str(value) |
538 | 534 | raise exceptions.ValidationError(msg) |
| 535 | return value |
539 | 536 | |
540 | 537 | def validate(self, value, model_instance): |
541 | 538 | pass |
542 | 539 | |
543 | 540 | def get_prep_value(self, value): |
544 | | if value is None: |
545 | | return None |
546 | | return int(value) |
| 541 | return value |
| 542 | |
| 543 | def get_db_prep_value(self, value, connection, prepared=False): |
| 544 | # Casts AutoField into the format expected by the backend |
| 545 | if not prepared: |
| 546 | value = self.get_prep_value(value) |
| 547 | return connection.ops.value_to_db_auto(value) |
547 | 548 | |
548 | 549 | def contribute_to_class(self, cls, name): |
549 | 550 | assert not cls._meta.has_auto_field, \ |
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index 848fd6e..c6cffec 100644
a
|
b
|
|
1 | 1 | from operator import attrgetter |
2 | 2 | |
3 | | from django.db import connection, router |
| 3 | from django.db import connection, router, connections |
4 | 4 | from django.db.backends import util |
5 | 5 | from django.db.models import signals, get_model |
6 | 6 | from django.db.models.fields import (AutoField, Field, IntegerField, |
… |
… |
class ForeignKey(RelatedField, Field):
|
960 | 960 | return None |
961 | 961 | else: |
962 | 962 | return self.rel.get_related_field().get_db_prep_save(value, |
963 | | connection=connection) |
| 963 | connection=connections[router.db_for_read(self.rel.to)]) |
964 | 964 | |
965 | 965 | def value_to_string(self, obj): |
966 | 966 | if not obj: |
… |
… |
class ForeignKey(RelatedField, Field):
|
1015 | 1015 | # If the database needs similar types for key fields however, the only |
1016 | 1016 | # thing we can do is making AutoField an IntegerField. |
1017 | 1017 | rel_field = self.rel.get_related_field() |
1018 | | if (isinstance(rel_field, AutoField) or |
1019 | | (not connection.features.related_fields_match_type and |
1020 | | isinstance(rel_field, (PositiveIntegerField, |
1021 | | PositiveSmallIntegerField)))): |
1022 | | return IntegerField().db_type(connection=connection) |
1023 | | return rel_field.db_type(connection=connection) |
| 1018 | return rel_field.related_db_type(connection=connections[router.db_for_read(rel_field.model)]) |
1024 | 1019 | |
1025 | 1020 | class OneToOneField(ForeignKey): |
1026 | 1021 | """ |