Fixed #26320 -- Deprecated implicit OneToOnField parent_link.

This commit is contained in:
Tim Graham 2016-04-22 12:59:41 -04:00
parent 9e4e20a71c
commit 87338198e9
8 changed files with 40 additions and 22 deletions

View File

@ -255,6 +255,11 @@ class Options(object):
field = already_created[0] field = already_created[0]
field.primary_key = True field.primary_key = True
self.setup_pk(field) self.setup_pk(field)
if not field.remote_field.parent_link:
warnings.warn(
'Add parent_link=True to %s as an implicit link is '
'deprecated.' % field, RemovedInDjango20Warning
)
else: else:
auto = AutoField(verbose_name='ID', primary_key=True, auto_created=True) auto = AutoField(verbose_name='ID', primary_key=True, auto_created=True)
model.add_to_class('id', auto) model.add_to_class('id', auto)

View File

@ -160,6 +160,9 @@ details on these changes.
* The ``django.contrib.gis.utils.precision_wkt()`` function will be removed. * The ``django.contrib.gis.utils.precision_wkt()`` function will be removed.
* In multi-table inheritance, implicit promotion of a ``OneToOneField`` to a
``parent_link`` will be removed.
.. _deprecation-removed-in-1.10: .. _deprecation-removed-in-1.10:
1.10 1.10

View File

@ -980,6 +980,9 @@ Miscellaneous
favor of class-based views :class:`~django.views.i18n.JavaScriptCatalog` favor of class-based views :class:`~django.views.i18n.JavaScriptCatalog`
and :class:`~django.views.i18n.JSONCatalog`. and :class:`~django.views.i18n.JSONCatalog`.
* In multi-table inheritance, implicit promotion of a ``OneToOneField`` to a
``parent_link`` is deprecated. Add ``parent_link=True`` to such fields.
.. _removed-features-1.10: .. _removed-features-1.10:
Features removed in 1.10 Features removed in 1.10

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import unittest import unittest
import warnings
from django.conf import settings from django.conf import settings
from django.core.checks import Error from django.core.checks import Error
@ -739,3 +740,24 @@ class OtherModelTests(SimpleTestCase):
) )
] ]
self.assertEqual(errors, expected) self.assertEqual(errors, expected)
def test_missing_parent_link(self):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter('always')
class Place(models.Model):
pass
class ParkingLot(Place):
# In lieu of any other connector, an existing OneToOneField will be
# promoted to the primary key.
parent = models.OneToOneField(Place, models.CASCADE)
self.assertEqual(len(warns), 1)
msg = str(warns[0].message)
self.assertEqual(
msg,
'Add parent_link=True to invalid_models_tests.ParkingLot.parent '
'as an implicit link is deprecated.'
)
self.assertEqual(ParkingLot._meta.pk.name, 'parent')

View File

@ -123,6 +123,7 @@ class OperationTestBase(MigrationTestBase):
'Pony', 'Pony',
models.CASCADE, models.CASCADE,
auto_created=True, auto_created=True,
parent_link=True,
primary_key=True, primary_key=True,
to_field='id', to_field='id',
serialize=False, serialize=False,

View File

@ -45,12 +45,6 @@ class ParkingLot(Place):
return "%s the parking lot" % self.name return "%s the parking lot" % self.name
class ParkingLot2(Place):
# In lieu of any other connector, an existing OneToOneField will be
# promoted to the primary key.
parent = models.OneToOneField(Place, models.CASCADE)
class ParkingLot3(Place): class ParkingLot3(Place):
# The parent_link connector need not be the pk on the model. # The parent_link connector need not be the pk on the model.
primary_key = models.AutoField(primary_key=True) primary_key = models.AutoField(primary_key=True)

View File

@ -13,10 +13,9 @@ from django.test import TestCase
from .models import ( from .models import (
ArticleWithAuthor, BachelorParty, BirthdayParty, BusStation, Child, ArticleWithAuthor, BachelorParty, BirthdayParty, BusStation, Child,
DerivedM, InternalCertificationAudit, ItalianRestaurant, M2MChild, DerivedM, InternalCertificationAudit, ItalianRestaurant, M2MChild,
MessyBachelorParty, ParkingLot, ParkingLot2, ParkingLot3, ParkingLot4A, MessyBachelorParty, ParkingLot, ParkingLot3, ParkingLot4A, ParkingLot4B,
ParkingLot4B, Person, Place, Profile, QualityControl, Restaurant, Person, Place, Profile, QualityControl, Restaurant, SelfRefChild,
SelfRefChild, SelfRefParent, Senator, Supplier, TrainStation, User, SelfRefParent, Senator, Supplier, TrainStation, User, Wholesaler,
Wholesaler,
) )
@ -293,20 +292,11 @@ class ModelInheritanceTest(TestCase):
def test_use_explicit_o2o_to_parent_as_pk(self): def test_use_explicit_o2o_to_parent_as_pk(self):
""" """
Regression tests for #10406 The connector from child to parent need not be the pk on the child.
If there's a one-to-one link between a child model and the parent and
no explicit pk declared, we can use the one-to-one link as the pk on
the child.
""" """
self.assertEqual(ParkingLot2._meta.pk.name, "parent")
# However, the connector from child to parent need not be the pk on
# the child at all.
self.assertEqual(ParkingLot3._meta.pk.name, "primary_key") self.assertEqual(ParkingLot3._meta.pk.name, "primary_key")
# the child->parent link # the child->parent link
self.assertEqual( self.assertEqual(ParkingLot3._meta.get_ancestor_link(Place).name, "parent")
ParkingLot3._meta.get_ancestor_link(Place).name,
"parent")
def test_use_explicit_o2o_to_parent_from_abstract_model(self): def test_use_explicit_o2o_to_parent_from_abstract_model(self):
self.assertEqual(ParkingLot4A._meta.pk.name, "parent") self.assertEqual(ParkingLot4A._meta.pk.name, "parent")

View File

@ -301,7 +301,7 @@ class InheritBaseModel(BaseModel):
class ExplicitInheritBaseModel(BaseModel): class ExplicitInheritBaseModel(BaseModel):
parent = models.OneToOneField(BaseModel, models.CASCADE) parent = models.OneToOneField(BaseModel, models.CASCADE, parent_link=True)
child_data = models.IntegerField() child_data = models.IntegerField()