Ticket #17337: nonint-autoid.patch

File nonint-autoid.patch, 3.7 KB (added by Jonas H., 13 years ago)
  • django/db/backends/__init__.py

    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):  
    721721    # need not necessarily be implemented using "LIKE" in the backend.
    722722    prep_for_iexact_query = prep_for_like_query
    723723
     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
    724733    def value_to_db_date(self, value):
    725734        """
    726735        Transform a date value to an object compatible with what is expected
  • django/db/models/fields/__init__.py

    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):  
    529529        return db_type
    530530
    531531    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))):
    537533            msg = self.error_messages['invalid'] % str(value)
    538534            raise exceptions.ValidationError(msg)
     535        return value
    539536
    540537    def validate(self, value, model_instance):
    541538        pass
    542539
    543540    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)
    547548
    548549    def contribute_to_class(self, cls, name):
    549550        assert not cls._meta.has_auto_field, \
  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 848fd6e..c6cffec 100644
    a b  
    11from operator import attrgetter
    22
    3 from django.db import connection, router
     3from django.db import connection, router, connections
    44from django.db.backends import util
    55from django.db.models import signals, get_model
    66from django.db.models.fields import (AutoField, Field, IntegerField,
    class ForeignKey(RelatedField, Field):  
    960960            return None
    961961        else:
    962962            return self.rel.get_related_field().get_db_prep_save(value,
    963                 connection=connection)
     963                    connection=connections[router.db_for_read(self.rel.to)])
    964964
    965965    def value_to_string(self, obj):
    966966        if not obj:
    class ForeignKey(RelatedField, Field):  
    10151015        # If the database needs similar types for key fields however, the only
    10161016        # thing we can do is making AutoField an IntegerField.
    10171017        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)])
    10241019
    10251020class OneToOneField(ForeignKey):
    10261021    """
Back to Top