summaryrefslogtreecommitdiff
path: root/tests/invalid_models_tests/test_models.py
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2020-01-16 08:06:16 +0100
committerGitHub <noreply@github.com>2020-01-16 08:06:16 +0100
commitbf77669453b9e9f64291da6701fe06fd5ba47e29 (patch)
treea2d13e239a715418a699979ad080c01beba62819 /tests/invalid_models_tests/test_models.py
parent7400da49a5d0ec5a087c3cf05e0b2d15048fc93f (diff)
Fixed #29998 -- Allowed multiple OneToOneFields to the parent model.
We assumed that any OneToOneField's in a child model must be the parent link and raised an error when parent_link=True was not specified. This patch allows to specify multiple OneToOneField's to the parent model. OneToOneField's without a custom related_name will raise fields.E304 and fields.E305 so this should warn users when they try to override the auto-created OneToOneField.
Diffstat (limited to 'tests/invalid_models_tests/test_models.py')
-rw-r--r--tests/invalid_models_tests/test_models.py25
1 files changed, 17 insertions, 8 deletions
diff --git a/tests/invalid_models_tests/test_models.py b/tests/invalid_models_tests/test_models.py
index 60b89b6f2e..ec2d345d5a 100644
--- a/tests/invalid_models_tests/test_models.py
+++ b/tests/invalid_models_tests/test_models.py
@@ -3,7 +3,6 @@ import unittest
from django.conf import settings
from django.core.checks import Error, Warning
from django.core.checks.model_checks import _check_lazy_references
-from django.core.exceptions import ImproperlyConfigured
from django.db import connection, connections, models
from django.db.models.functions import Lower
from django.db.models.signals import post_init
@@ -1006,14 +1005,24 @@ class OtherModelTests(SimpleTestCase):
self.assertEqual(ShippingMethod.check(), [])
- def test_missing_parent_link(self):
- msg = 'Add parent_link=True to invalid_models_tests.ParkingLot.parent.'
- with self.assertRaisesMessage(ImproperlyConfigured, msg):
- class Place(models.Model):
- pass
+ def test_onetoone_with_parent_model(self):
+ class Place(models.Model):
+ pass
+
+ class ParkingLot(Place):
+ other_place = models.OneToOneField(Place, models.CASCADE, related_name='other_parking')
+
+ self.assertEqual(ParkingLot.check(), [])
+
+ def test_onetoone_with_explicit_parent_link_parent_model(self):
+ class Place(models.Model):
+ pass
+
+ class ParkingLot(Place):
+ place = models.OneToOneField(Place, models.CASCADE, parent_link=True, primary_key=True)
+ other_place = models.OneToOneField(Place, models.CASCADE, related_name='other_parking')
- class ParkingLot(Place):
- parent = models.OneToOneField(Place, models.CASCADE)
+ self.assertEqual(ParkingLot.check(), [])
def test_m2m_table_name_clash(self):
class Foo(models.Model):