1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #27532 -- Deprecated Model._meta.has_auto_field

This commit is contained in:
Adam Chainz 2016-11-23 22:04:10 +00:00 committed by Tim Graham
parent cb7bbf97a7
commit 6252fd6314
7 changed files with 46 additions and 8 deletions

View File

@ -350,12 +350,12 @@ class InlineAdminForm(AdminForm):
def needs_explicit_pk_field(self): def needs_explicit_pk_field(self):
# Auto fields are editable (oddly), so need to check for auto or non-editable pk # Auto fields are editable (oddly), so need to check for auto or non-editable pk
if self.form._meta.model._meta.has_auto_field or not self.form._meta.model._meta.pk.editable: if self.form._meta.model._meta.auto_field or not self.form._meta.model._meta.pk.editable:
return True return True
# Also search any parents for an auto field. (The pk info is propagated to child # Also search any parents for an auto field. (The pk info is propagated to child
# models so that does not need to be checked in parents.) # models so that does not need to be checked in parents.)
for parent in self.form._meta.model._meta.get_parent_list(): for parent in self.form._meta.model._meta.get_parent_list():
if parent._meta.has_auto_field: if parent._meta.auto_field:
return True return True
return False return False

View File

@ -900,7 +900,7 @@ class Model(six.with_metaclass(ModelBase)):
if not pk_set: if not pk_set:
fields = [f for f in fields if f is not meta.auto_field] fields = [f for f in fields if f is not meta.auto_field]
update_pk = bool(meta.has_auto_field and not pk_set) update_pk = meta.auto_field and not pk_set
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
if update_pk: if update_pk:
setattr(self, meta.pk.attname, result) setattr(self, meta.pk.attname, result)

View File

@ -948,10 +948,8 @@ class AutoField(Field):
return int(value) return int(value)
def contribute_to_class(self, cls, name, **kwargs): def contribute_to_class(self, cls, name, **kwargs):
assert not cls._meta.has_auto_field, \ assert not cls._meta.auto_field, "A model can't have more than one AutoField."
"A model can't have more than one AutoField."
super(AutoField, self).contribute_to_class(cls, name, **kwargs) super(AutoField, self).contribute_to_class(cls, name, **kwargs)
cls._meta.has_auto_field = True
cls._meta.auto_field = self cls._meta.auto_field = self
def formfield(self, **kwargs): def formfield(self, **kwargs):

View File

@ -17,7 +17,8 @@ from django.db.models.fields.related import OneToOneField
from django.utils import six from django.utils import six
from django.utils.datastructures import ImmutableList, OrderedSet from django.utils.datastructures import ImmutableList, OrderedSet
from django.utils.deprecation import ( from django.utils.deprecation import (
RemovedInDjango20Warning, warn_about_renamed_method, RemovedInDjango20Warning, RemovedInDjango21Warning,
warn_about_renamed_method,
) )
from django.utils.encoding import force_text, python_2_unicode_compatible from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.functional import cached_property from django.utils.functional import cached_property
@ -113,7 +114,6 @@ class Options(object):
self.required_db_vendor = None self.required_db_vendor = None
self.meta = meta self.meta = meta
self.pk = None self.pk = None
self.has_auto_field = False
self.auto_field = None self.auto_field = None
self.abstract = False self.abstract = False
self.managed = True self.managed = True
@ -825,3 +825,16 @@ class Options(object):
# Store result into cache for later access # Store result into cache for later access
self._get_fields_cache[cache_key] = fields self._get_fields_cache[cache_key] = fields
return fields return fields
@property
def has_auto_field(self):
warnings.warn(
'Model._meta.has_auto_field is deprecated in favor of checking if '
'Model._meta.auto_field is not None.',
RemovedInDjango21Warning, stacklevel=2
)
return self.auto_field is not None
@has_auto_field.setter
def has_auto_field(self, value):
pass

View File

@ -46,6 +46,8 @@ details on these changes.
* The ``USE_ETAGS`` setting will be removed. ``CommonMiddleware`` and * The ``USE_ETAGS`` setting will be removed. ``CommonMiddleware`` and
``django.utils.cache.patch_response_headers()`` will no longer set ETags. ``django.utils.cache.patch_response_headers()`` will no longer set ETags.
* The ``Model._meta.has_auto_field`` attribute will be removed.
.. _deprecation-removed-in-2.0: .. _deprecation-removed-in-2.0:
2.0 2.0

View File

@ -707,3 +707,6 @@ Miscellaneous
``ETag`` header to responses regardless of the setting. ``CommonMiddleware`` ``ETag`` header to responses regardless of the setting. ``CommonMiddleware``
and ``django.utils.cache.patch_response_headers()`` will no longer set ETags and ``django.utils.cache.patch_response_headers()`` will no longer set ETags
when the deprecation ends. when the deprecation ends.
* ``Model._meta.has_auto_field`` is deprecated in favor of checking if
``Model._meta.auto_field is not None``.

View File

@ -0,0 +1,22 @@
import warnings
from django.test import SimpleTestCase
from .models import Person
class HasAutoFieldTests(SimpleTestCase):
def test_get_warns(self):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter('always')
Person._meta.has_auto_field
self.assertEqual(len(warns), 1)
self.assertEqual(
str(warns[0].message),
'Model._meta.has_auto_field is deprecated in favor of checking if '
'Model._meta.auto_field is not None.',
)
def test_set_does_nothing(self):
Person._meta.has_auto_field = True