Ticket #24042: patch.diff
File patch.diff, 11.9 KB (added by , 10 years ago) |
---|
-
django/db/backends/__init__.py
diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 104744b..a3c4317 100644
a b class BaseDatabaseIntrospection(object): 1400 1400 def sequence_list(self): 1401 1401 "Returns a list of information about all DB sequences for all models in all apps." 1402 1402 from django.apps import apps 1403 from django.db import models,router1403 from django.db import router 1404 1404 1405 1405 sequence_list = [] 1406 1406 … … class BaseDatabaseIntrospection(object): 1411 1411 if model._meta.swapped: 1412 1412 continue 1413 1413 for f in model._meta.local_fields: 1414 if isinstance(f, models.AutoField):1414 if f.get_internal_type() == 'AutoField': 1415 1415 sequence_list.append({'table': model._meta.db_table, 'column': f.column}) 1416 1416 break # Only one AutoField is allowed per model, so don't bother continuing. 1417 1417 -
django/db/backends/oracle/base.py
diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index 9f275b7..c7a162c 100644
a b WHEN (new.%(col_name)s IS NULL) 444 444 return sql 445 445 446 446 def sequence_reset_sql(self, style, model_list): 447 from django.db import models448 447 output = [] 449 448 query = _get_sequence_reset_sql() 450 449 for model in model_list: 451 450 for f in model._meta.local_fields: 452 if isinstance(f, models.AutoField):451 if f.get_internal_type() == 'AutoField': 453 452 table_name = self.quote_name(model._meta.db_table) 454 453 sequence_name = self._get_sequence_name(model._meta.db_table) 455 454 column_name = self.quote_name(f.column) -
django/db/backends/postgresql_psycopg2/operations.py
diff --git a/django/db/backends/postgresql_psycopg2/operations.py b/django/db/backends/postgresql_psycopg2/operations.py index 3447bfe..181d8c4 100644
a b class DatabaseOperations(BaseDatabaseOperations): 135 135 return "TABLESPACE %s" % self.quote_name(tablespace) 136 136 137 137 def sequence_reset_sql(self, style, model_list): 138 from django.db import models139 138 output = [] 140 139 qn = self.quote_name 141 140 for model in model_list: … … class DatabaseOperations(BaseDatabaseOperations): 146 145 # and column name (available since PostgreSQL 8) 147 146 148 147 for f in model._meta.local_fields: 149 if isinstance(f, models.AutoField):148 if f.get_internal_type() == 'AutoField': 150 149 output.append( 151 150 "%s setval(pg_get_serial_sequence('%s','%s'), " 152 151 "coalesce(max(%s), 1), max(%s) %s null) %s %s;" % ( -
django/db/models/base.py
diff --git a/django/db/models/base.py b/django/db/models/base.py index d0c1b7a..e68fb20 100644
a b from django.db import (router, connections, transaction, DatabaseError, 16 16 from django.db.models import signals 17 17 from django.db.models.constants import LOOKUP_SEP 18 18 from django.db.models.deletion import Collector 19 from django.db.models.fields import AutoField,FieldDoesNotExist19 from django.db.models.fields import FieldDoesNotExist 20 20 from django.db.models.fields.related import (ForeignObjectRel, ManyToOneRel, 21 21 OneToOneField, add_lazy_relation) 22 22 from django.db.models.manager import ensure_default_manager … … class Model(six.with_metaclass(ModelBase)): 797 797 798 798 fields = meta.local_concrete_fields 799 799 if not pk_set: 800 fields = [f for f in fields if not isinstance(f, AutoField)]800 fields = [f for f in fields if not f.get_internal_type() == "AutoField"] 801 801 802 802 update_pk = bool(meta.has_auto_field and not pk_set) 803 803 result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) -
django/db/models/fields/related.py
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index a2f4b95..dc563fb 100644
a b from django.db import connection, connections, router, transaction 8 8 from django.db.backends import utils 9 9 from django.db.models import signals, Q 10 10 from django.db.models.deletion import SET_NULL, SET_DEFAULT, CASCADE 11 from django.db.models.fields import ( AutoField,Field, IntegerField,11 from django.db.models.fields import (Field, IntegerField, 12 12 PositiveIntegerField, PositiveSmallIntegerField, FieldDoesNotExist) 13 13 from django.db.models.lookups import IsNull 14 14 from django.db.models.related import RelatedObject, PathInfo … … class ForeignKey(ForeignObject): 1860 1860 # If the database needs similar types for key fields however, the only 1861 1861 # thing we can do is making AutoField an IntegerField. 1862 1862 rel_field = self.related_field 1863 if ( isinstance(rel_field, AutoField)or1863 if (rel_field.get_internal_type() == 'AutoField' or 1864 1864 (not connection.features.related_fields_match_type and 1865 1865 isinstance(rel_field, (PositiveIntegerField, 1866 1866 PositiveSmallIntegerField)))): -
django/db/models/query.py
diff --git a/django/db/models/query.py b/django/db/models/query.py index 42dfd47..b17a8fc 100644
a b from django.core import exceptions 12 12 from django.db import (connections, router, transaction, IntegrityError, 13 13 DJANGO_VERSION_PICKLE_KEY) 14 14 from django.db.models.constants import LOOKUP_SEP 15 from django.db.models.fields import AutoField,Empty15 from django.db.models.fields import Empty 16 16 from django.db.models.query_utils import (Q, select_related_descend, 17 17 deferred_class_factory, InvalidQuery) 18 18 from django.db.models.deletion import Collector … … class QuerySet(object): 424 424 if objs_with_pk: 425 425 self._batched_insert(objs_with_pk, fields, batch_size) 426 426 if objs_without_pk: 427 fields = [f for f in fields if not isinstance(f, AutoField)]427 fields = [f for f in fields if not f.get_internal_type() == 'AutoField'] 428 428 self._batched_insert(objs_without_pk, fields, batch_size) 429 429 430 430 return objs -
django/forms/models.py
diff --git a/django/forms/models.py b/django/forms/models.py index 7ca4954..58290ff 100644
a b def construct_instance(form, instance, fields=None, exclude=None): 45 45 cleaned_data = form.cleaned_data 46 46 file_field_list = [] 47 47 for f in opts.fields: 48 if not f.editable or isinstance(f, models.AutoField)\48 if not f.editable or f.get_internal_type() == 'AutoField' \ 49 49 or f.name not in cleaned_data: 50 50 continue 51 51 if fields is not None and f.name not in fields: … … class BaseModelFormSet(BaseFormSet): 773 773 774 774 def add_fields(self, form, index): 775 775 """Add a hidden field for the object's primary key.""" 776 from django.db.models import AutoField,OneToOneField, ForeignKey776 from django.db.models import OneToOneField, ForeignKey 777 777 self._pk_field = pk = self.model._meta.pk 778 778 # If a pk isn't editable, then it won't be on the form, so we need to 779 779 # add it here so we can tell which object is which when we get the … … class BaseModelFormSet(BaseFormSet): 782 782 # True, so check for that as well. 783 783 784 784 def pk_is_not_editable(pk): 785 return ((not pk.editable) or (pk.auto_created or isinstance(pk, AutoField)) 785 return ((not pk.editable) 786 or (pk.auto_created or pk.get_internal_type == 'AutoField') 786 787 or (pk.rel and pk.rel.parent_link and pk_is_not_editable(pk.rel.to._meta.pk))) 787 788 if pk_is_not_editable(pk) or pk.name not in form.fields: 788 789 if form.is_bound: -
tests/field_subclassing/fields.py
diff --git a/tests/field_subclassing/fields.py b/tests/field_subclassing/fields.py index b94b237..a738433 100644
a b from __future__ import unicode_literals 3 3 import json 4 4 import warnings 5 5 6 from django.core import exceptions 6 7 from django.db import models 7 8 from django.utils.encoding import force_text 8 9 from django.utils import six 9 10 from django.utils.deprecation import RemovedInDjango20Warning 10 11 from django.utils.encoding import python_2_unicode_compatible 12 from django.utils.translation import ugettext_lazy as _ 11 13 12 14 13 15 # Catch warning about subfieldbase -- remove in Django 2.0 … … class JSONField(six.with_metaclass(models.SubfieldBase, models.TextField)): 94 96 class CustomTypedField(models.TextField): 95 97 def db_type(self, connection): 96 98 return 'custom_field' 99 100 101 class CustomAutoField(models.Field): 102 description = _("Integer") 103 104 empty_strings_allowed = False 105 default_error_messages = { 106 'invalid': _("'%(value)s' value must be an integer."), 107 } 108 109 def __init__(self, *args, **kwargs): 110 kwargs['blank'] = True 111 super(CustomAutoField, self).__init__(*args, **kwargs) 112 113 def get_internal_type(self): 114 return "AutoField" 115 116 def to_python(self, value): 117 if value is None: 118 return value 119 try: 120 return int(value) 121 except (TypeError, ValueError): 122 raise exceptions.ValidationError( 123 self.error_messages['invalid'], 124 code='invalid', 125 params={'value': value}, 126 ) 127 128 def get_db_prep_value(self, value, connection, prepared=False): 129 if not prepared: 130 value = self.get_prep_value(value) 131 value = connection.ops.validate_autopk_value(value) 132 return value 133 134 def get_prep_value(self, value): 135 value = super(CustomAutoField, self).get_prep_value(value) 136 if value is None: 137 return None 138 return int(value) 139 140 def contribute_to_class(self, cls, name, **kwargs): 141 assert not cls._meta.has_auto_field, \ 142 "A model can't have more than one AutoField." 143 super(CustomAutoField, self).contribute_to_class(cls, name, **kwargs) 144 cls._meta.has_auto_field = True 145 cls._meta.auto_field = self 146 147 def formfield(self, **kwargs): 148 return None -
tests/field_subclassing/models.py
diff --git a/tests/field_subclassing/models.py b/tests/field_subclassing/models.py index 66e765a..6107228 100644
a b Tests for field subclassing. 4 4 from django.db import models 5 5 from django.utils.encoding import force_text 6 6 7 from .fields import Small, SmallField, SmallerField, JSONField 7 from .fields import Small, SmallField, SmallerField, JSONField, CustomAutoField 8 8 from django.utils.encoding import python_2_unicode_compatible 9 9 10 10 … … class ChoicesModel(models.Model): 33 33 34 34 class DataModel(models.Model): 35 35 data = JSONField() 36 37 38 class CustomAutoFieldModel(models.Model): 39 id = CustomAutoField(primary_key=True) -
tests/field_subclassing/tests.py
diff --git a/tests/field_subclassing/tests.py b/tests/field_subclassing/tests.py index 9e40d92..fac1997 100644
a b from django.db import connection 7 7 from django.test import TestCase 8 8 9 9 from .fields import Small, CustomTypedField 10 from .models import ChoicesModel, DataModel, MyModel, OtherModel 10 from .models import ChoicesModel, DataModel, MyModel, OtherModel, CustomAutoFieldModel 11 11 12 12 13 13 class CustomField(TestCase): … … class CustomField(TestCase): 122 122 with self.assertRaises(exceptions.ValidationError): 123 123 o.full_clean() 124 124 125 def test_auto_field(self): 126 o = CustomAutoFieldModel() 127 self.assertEqual(o.pk, None) 128 o.save() 129 self.assertNotEqual(o.pk, None) 130 self.assertTrue(isinstance(o.pk, int)) 131 125 132 126 133 class TestDbType(TestCase): 127 134