Ticket #16385: 043f2c08b87dda585d40a1ab225106543909f4a6.diff

File 043f2c08b87dda585d40a1ab225106543909f4a6.diff, 3.6 KB (added by jonash, 4 years ago)

AutoField/string-based primary keys

  • django/db/backends/__init__.py

    diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
    index 4487bde..01144eb 100644
    a b class BaseDatabaseOperations(object): 
    671671    # need not necessarily be implemented using "LIKE" in the backend.
    672672    prep_for_iexact_query = prep_for_like_query
    673673
     674    def value_to_db_auto(self, value):
     675        """
     676        Transform a value to an object compatible with the auto field required
     677        by the backend driver for auto columns.
     678        """
     679        if value is None:
     680            return None
     681        return int(value)
     682
    674683    def value_to_db_date(self, value):
    675684        """
    676685        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 d79c172..42cc98a 100644
    a b class AutoField(Field): 
    474474        return db_type
    475475
    476476    def to_python(self, value):
    477         if value is None:
    478             return value
    479         try:
    480             return int(value)
    481         except (TypeError, ValueError):
     477        if not (value is None or isinstance(value, (basestring, int, long))):
    482478            raise exceptions.ValidationError(self.error_messages['invalid'])
     479        return value
    483480
    484481    def validate(self, value, model_instance):
    485482        pass
    486483
    487484    def get_prep_value(self, value):
    488         if value is None:
    489             return None
    490         return int(value)
     485        return value
     486
     487    def get_db_prep_value(self, value, connection, prepared=False):
     488        # Casts AutoField into the format expected by the backend
     489        if not prepared:
     490            value = self.get_prep_value(value)
     491        return connection.ops.value_to_db_auto(value)
    491492
    492493    def contribute_to_class(self, cls, name):
    493494        assert not cls._meta.has_auto_field, "A model can't have more than one AutoField."
  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 1318706..7dceb34 100644
    a b  
    11from django.conf import settings
    2 from django.db import connection, router, transaction
     2from django.db import connection, router, transaction, connections
    33from django.db.backends import util
    44from django.db.models import signals, get_model
    55from django.db.models.fields import (AutoField, Field, IntegerField,
    class ForeignKey(RelatedField, Field): 
    873873            return None
    874874        else:
    875875            return self.rel.get_related_field().get_db_prep_save(value,
    876                 connection=connection)
     876                    connection=connections[router.db_for_read(self.rel.to)])
    877877
    878878    def value_to_string(self, obj):
    879879        if not obj:
    class ForeignKey(RelatedField, Field): 
    924924        # If the database needs similar types for key fields however, the only
    925925        # thing we can do is making AutoField an IntegerField.
    926926        rel_field = self.rel.get_related_field()
    927         if (isinstance(rel_field, AutoField) or
    928                 (not connection.features.related_fields_match_type and
    929                 isinstance(rel_field, (PositiveIntegerField,
    930                                        PositiveSmallIntegerField)))):
    931             return IntegerField().db_type(connection=connection)
    932         return rel_field.db_type(connection=connection)
     927        return rel_field.related_db_type(connection=connections[router.db_for_read(rel_field.model)])
    933928
    934929class OneToOneField(ForeignKey):
    935930    """
Back to Top