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/django/db/backends/__init__.py
+++ b/django/db/backends/__init__.py
@@ -721,6 +721,15 @@ class BaseDatabaseOperations(object):
     # need not necessarily be implemented using "LIKE" in the backend.
     prep_for_iexact_query = prep_for_like_query
 
+    def value_to_db_auto(self, value):
+        """
+        Transform a value to an object compatible with the auto field required
+        by the backend driver for auto columns.
+        """
+        if value is None:
+            return None
+        return int(value)
+
     def value_to_db_date(self, value):
         """
         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/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -529,21 +529,22 @@ class AutoField(Field):
         return db_type
 
     def to_python(self, value):
-        if value is None:
-            return value
-        try:
-            return int(value)
-        except (TypeError, ValueError):
+        if not (value is None or isinstance(value, (basestring, int, long))):
             msg = self.error_messages['invalid'] % str(value)
             raise exceptions.ValidationError(msg)
+        return value
 
     def validate(self, value, model_instance):
         pass
 
     def get_prep_value(self, value):
-        if value is None:
-            return None
-        return int(value)
+        return value
+
+    def get_db_prep_value(self, value, connection, prepared=False):
+        # Casts AutoField into the format expected by the backend
+        if not prepared:
+            value = self.get_prep_value(value)
+        return connection.ops.value_to_db_auto(value)
 
     def contribute_to_class(self, cls, name):
         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/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -1,6 +1,6 @@
 from operator import attrgetter
 
-from django.db import connection, router
+from django.db import connection, router, connections
 from django.db.backends import util
 from django.db.models import signals, get_model
 from django.db.models.fields import (AutoField, Field, IntegerField,
@@ -960,7 +960,7 @@ class ForeignKey(RelatedField, Field):
             return None
         else:
             return self.rel.get_related_field().get_db_prep_save(value,
-                connection=connection)
+                    connection=connections[router.db_for_read(self.rel.to)])
 
     def value_to_string(self, obj):
         if not obj:
@@ -1015,12 +1015,7 @@ class ForeignKey(RelatedField, Field):
         # If the database needs similar types for key fields however, the only
         # thing we can do is making AutoField an IntegerField.
         rel_field = self.rel.get_related_field()
-        if (isinstance(rel_field, AutoField) or
-                (not connection.features.related_fields_match_type and
-                isinstance(rel_field, (PositiveIntegerField,
-                                       PositiveSmallIntegerField)))):
-            return IntegerField().db_type(connection=connection)
-        return rel_field.db_type(connection=connection)
+        return rel_field.related_db_type(connection=connections[router.db_for_read(rel_field.model)])
 
 class OneToOneField(ForeignKey):
     """
