2019-01-12 21:14:54 +00:00
|
|
|
|
from unittest import mock
|
|
|
|
|
|
2014-07-08 20:42:40 +00:00
|
|
|
|
from django.core.checks import Error
|
|
|
|
|
from django.core.checks import Warning as DjangoWarning
|
2019-01-12 21:14:54 +00:00
|
|
|
|
from django.db import connection, models
|
|
|
|
|
from django.test.testcases import SimpleTestCase
|
2020-12-30 02:22:17 +00:00
|
|
|
|
from django.test.utils import isolate_apps, modify_settings, override_settings
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
class RelativeFieldTests(SimpleTestCase):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
def test_valid_foreign_key_without_accessor(self):
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
# There would be a clash if Model.field installed an accessor.
|
|
|
|
|
model = models.IntegerField()
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
field = models.ForeignKey(Target, models.CASCADE, related_name="+")
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
field = Model._meta.get_field("field")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(field.check(), [])
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_foreign_key_to_missing_model(self):
|
|
|
|
|
# Model names are resolved when a model is being created, so we cannot
|
|
|
|
|
# test relative fields in isolation and we need to attach them to a
|
|
|
|
|
# model.
|
|
|
|
|
class Model(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
foreign_key = models.ForeignKey("Rel1", models.CASCADE)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
field = Model._meta.get_field("foreign_key")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2016-02-12 16:36:46 +00:00
|
|
|
|
"Field defines a relation with model 'Rel1', "
|
|
|
|
|
"which is either not installed, or is abstract.",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E300",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
def test_foreign_key_to_isolate_apps_model(self):
|
2015-11-09 21:50:40 +00:00
|
|
|
|
"""
|
|
|
|
|
#25723 - Referenced model registration lookup should be run against the
|
|
|
|
|
field's model registry.
|
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2015-11-09 21:50:40 +00:00
|
|
|
|
class OtherModel(models.Model):
|
2015-11-17 05:39:28 +00:00
|
|
|
|
pass
|
2015-11-09 21:50:40 +00:00
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
foreign_key = models.ForeignKey("OtherModel", models.CASCADE)
|
|
|
|
|
|
|
|
|
|
field = Model._meta.get_field("foreign_key")
|
|
|
|
|
self.assertEqual(field.check(from_model=Model), [])
|
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
def test_many_to_many_to_missing_model(self):
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
m2m = models.ManyToManyField("Rel2")
|
|
|
|
|
|
|
|
|
|
field = Model._meta.get_field("m2m")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Model),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2016-02-12 16:36:46 +00:00
|
|
|
|
"Field defines a relation with model 'Rel2', "
|
|
|
|
|
"which is either not installed, or is abstract.",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E300",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
def test_many_to_many_to_isolate_apps_model(self):
|
2015-11-09 21:50:40 +00:00
|
|
|
|
"""
|
|
|
|
|
#25723 - Referenced model registration lookup should be run against the
|
|
|
|
|
field's model registry.
|
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2015-11-09 21:50:40 +00:00
|
|
|
|
class OtherModel(models.Model):
|
2015-11-17 05:39:28 +00:00
|
|
|
|
pass
|
2015-11-09 21:50:40 +00:00
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
m2m = models.ManyToManyField("OtherModel")
|
|
|
|
|
|
|
|
|
|
field = Model._meta.get_field("m2m")
|
|
|
|
|
self.assertEqual(field.check(from_model=Model), [])
|
|
|
|
|
|
2024-07-05 18:41:01 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
def test_auto_created_through_model(self):
|
|
|
|
|
class OtherModel(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class M2MModel(models.Model):
|
|
|
|
|
many_to_many_rel = models.ManyToManyField(OtherModel)
|
|
|
|
|
|
|
|
|
|
class O2OModel(models.Model):
|
|
|
|
|
one_to_one_rel = models.OneToOneField(
|
|
|
|
|
"invalid_models_tests.M2MModel_many_to_many_rel",
|
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
field = O2OModel._meta.get_field("one_to_one_rel")
|
|
|
|
|
self.assertEqual(field.check(from_model=O2OModel), [])
|
|
|
|
|
|
2014-07-08 20:42:40 +00:00
|
|
|
|
def test_many_to_many_with_useless_options(self):
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
name = models.CharField(max_length=20)
|
|
|
|
|
|
|
|
|
|
class ModelM2M(models.Model):
|
2022-10-16 05:59:39 +00:00
|
|
|
|
m2m = models.ManyToManyField(
|
|
|
|
|
Model, null=True, validators=[lambda x: x], db_comment="Column comment"
|
|
|
|
|
)
|
2014-07-08 20:42:40 +00:00
|
|
|
|
|
|
|
|
|
field = ModelM2M._meta.get_field("m2m")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
ModelM2M.check(),
|
|
|
|
|
[
|
2014-07-08 20:42:40 +00:00
|
|
|
|
DjangoWarning(
|
|
|
|
|
"null has no effect on ManyToManyField.",
|
|
|
|
|
obj=field,
|
|
|
|
|
id="fields.W340",
|
2016-07-08 23:37:40 +00:00
|
|
|
|
),
|
2014-07-08 20:42:40 +00:00
|
|
|
|
DjangoWarning(
|
|
|
|
|
"ManyToManyField does not support validators.",
|
|
|
|
|
obj=field,
|
|
|
|
|
id="fields.W341",
|
2016-07-08 23:37:40 +00:00
|
|
|
|
),
|
2022-10-16 05:59:39 +00:00
|
|
|
|
DjangoWarning(
|
|
|
|
|
"db_comment has no effect on ManyToManyField.",
|
|
|
|
|
obj=field,
|
|
|
|
|
id="fields.W346",
|
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-07-08 20:42:40 +00:00
|
|
|
|
|
2021-08-04 20:19:29 +00:00
|
|
|
|
def test_many_to_many_with_useless_related_name(self):
|
|
|
|
|
class ModelM2M(models.Model):
|
|
|
|
|
m2m = models.ManyToManyField("self", related_name="children")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2021-08-04 20:19:29 +00:00
|
|
|
|
field = ModelM2M._meta.get_field("m2m")
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
ModelM2M.check(),
|
|
|
|
|
[
|
|
|
|
|
DjangoWarning(
|
|
|
|
|
"related_name has no effect on ManyToManyField with "
|
|
|
|
|
'a symmetrical relationship, e.g. to "self".',
|
|
|
|
|
obj=field,
|
|
|
|
|
id="fields.W345",
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
2020-02-27 15:57:53 +00:00
|
|
|
|
def test_ambiguous_relationship_model_from(self):
|
|
|
|
|
class Person(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Group(models.Model):
|
|
|
|
|
field = models.ManyToManyField("Person", through="AmbiguousRelationship")
|
|
|
|
|
|
|
|
|
|
class AmbiguousRelationship(models.Model):
|
|
|
|
|
person = models.ForeignKey(Person, models.CASCADE)
|
|
|
|
|
first_group = models.ForeignKey(Group, models.CASCADE, related_name="first")
|
|
|
|
|
second_group = models.ForeignKey(
|
|
|
|
|
Group, models.CASCADE, related_name="second"
|
|
|
|
|
)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2020-02-27 15:57:53 +00:00
|
|
|
|
field = Group._meta.get_field("field")
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Group),
|
|
|
|
|
[
|
|
|
|
|
Error(
|
|
|
|
|
"The model is used as an intermediate model by "
|
|
|
|
|
"'invalid_models_tests.Group.field', but it has more than one "
|
|
|
|
|
"foreign key from 'Group', which is ambiguous. You must "
|
|
|
|
|
"specify which foreign key Django should use via the "
|
|
|
|
|
"through_fields keyword argument.",
|
|
|
|
|
hint=(
|
|
|
|
|
"If you want to create a recursive relationship, use "
|
2020-02-26 18:08:55 +00:00
|
|
|
|
'ManyToManyField("self", through="AmbiguousRelationship").'
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2020-02-27 15:57:53 +00:00
|
|
|
|
id="fields.E334",
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
2020-02-26 18:08:55 +00:00
|
|
|
|
def test_ambiguous_relationship_model_to(self):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
class Person(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Group(models.Model):
|
2016-02-12 16:36:46 +00:00
|
|
|
|
field = models.ManyToManyField(
|
|
|
|
|
"Person", through="AmbiguousRelationship", related_name="tertiary"
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
class AmbiguousRelationship(models.Model):
|
|
|
|
|
# Too much foreign keys to Person.
|
2015-07-22 14:43:21 +00:00
|
|
|
|
first_person = models.ForeignKey(
|
|
|
|
|
Person, models.CASCADE, related_name="first"
|
|
|
|
|
)
|
|
|
|
|
second_person = models.ForeignKey(
|
|
|
|
|
Person, models.CASCADE, related_name="second"
|
|
|
|
|
)
|
|
|
|
|
second_model = models.ForeignKey(Group, models.CASCADE)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
field = Group._meta.get_field("field")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Group),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2016-02-12 16:36:46 +00:00
|
|
|
|
"The model is used as an intermediate model by "
|
|
|
|
|
"'invalid_models_tests.Group.field', but it has more than one "
|
|
|
|
|
"foreign key to 'Person', which is ambiguous. You must specify "
|
|
|
|
|
"which foreign key Django should use via the through_fields "
|
|
|
|
|
"keyword argument.",
|
|
|
|
|
hint=(
|
|
|
|
|
"If you want to create a recursive relationship, use "
|
2020-02-26 18:08:55 +00:00
|
|
|
|
'ManyToManyField("self", through="AmbiguousRelationship").'
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E335",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_relationship_model_with_foreign_key_to_wrong_model(self):
|
|
|
|
|
class WrongModel(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Person(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Group(models.Model):
|
2016-02-12 16:36:46 +00:00
|
|
|
|
members = models.ManyToManyField("Person", through="InvalidRelationship")
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
class InvalidRelationship(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
person = models.ForeignKey(Person, models.CASCADE)
|
|
|
|
|
wrong_foreign_key = models.ForeignKey(WrongModel, models.CASCADE)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
# The last foreign key should point to Group model.
|
|
|
|
|
|
|
|
|
|
field = Group._meta.get_field("members")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Group),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2016-02-12 16:36:46 +00:00
|
|
|
|
"The model is used as an intermediate model by "
|
|
|
|
|
"'invalid_models_tests.Group.members', but it does not "
|
|
|
|
|
"have a foreign key to 'Group' or 'Person'.",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=InvalidRelationship,
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E336",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_relationship_model_missing_foreign_key(self):
|
|
|
|
|
class Person(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Group(models.Model):
|
2016-02-12 16:36:46 +00:00
|
|
|
|
members = models.ManyToManyField("Person", through="InvalidRelationship")
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
class InvalidRelationship(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
group = models.ForeignKey(Group, models.CASCADE)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
# No foreign key to Person
|
|
|
|
|
|
|
|
|
|
field = Group._meta.get_field("members")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Group),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2016-02-12 16:36:46 +00:00
|
|
|
|
"The model is used as an intermediate model by "
|
|
|
|
|
"'invalid_models_tests.Group.members', but it does not have "
|
|
|
|
|
"a foreign key to 'Group' or 'Person'.",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=InvalidRelationship,
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E336",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_missing_relationship_model(self):
|
|
|
|
|
class Person(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Group(models.Model):
|
2016-02-12 16:36:46 +00:00
|
|
|
|
members = models.ManyToManyField("Person", through="MissingM2MModel")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
field = Group._meta.get_field("members")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Group),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2016-02-12 16:36:46 +00:00
|
|
|
|
"Field specifies a many-to-many relation through model "
|
|
|
|
|
"'MissingM2MModel', which has not been installed.",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E331",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2016-06-30 11:22:10 +00:00
|
|
|
|
def test_missing_relationship_model_on_model_check(self):
|
|
|
|
|
class Person(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Group(models.Model):
|
|
|
|
|
members = models.ManyToManyField("Person", through="MissingM2MModel")
|
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
2016-06-30 11:22:10 +00:00
|
|
|
|
Group.check(),
|
2022-02-03 19:24:19 +00:00
|
|
|
|
[
|
|
|
|
|
Error(
|
2016-06-30 11:22:10 +00:00
|
|
|
|
"Field specifies a many-to-many relation through model "
|
|
|
|
|
"'MissingM2MModel', which has not been installed.",
|
|
|
|
|
obj=Group._meta.get_field("members"),
|
|
|
|
|
id="fields.E331",
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
def test_many_to_many_through_isolate_apps_model(self):
|
2015-11-09 21:50:40 +00:00
|
|
|
|
"""
|
|
|
|
|
#25723 - Through model registration lookup should be run against the
|
|
|
|
|
field's model registry.
|
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2015-11-09 21:50:40 +00:00
|
|
|
|
class GroupMember(models.Model):
|
|
|
|
|
person = models.ForeignKey("Person", models.CASCADE)
|
|
|
|
|
group = models.ForeignKey("Group", models.CASCADE)
|
|
|
|
|
|
|
|
|
|
class Person(models.Model):
|
2015-11-17 05:39:28 +00:00
|
|
|
|
pass
|
2015-11-09 21:50:40 +00:00
|
|
|
|
|
|
|
|
|
class Group(models.Model):
|
|
|
|
|
members = models.ManyToManyField("Person", through="GroupMember")
|
|
|
|
|
|
|
|
|
|
field = Group._meta.get_field("members")
|
|
|
|
|
self.assertEqual(field.check(from_model=Group), [])
|
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
def test_too_many_foreign_keys_in_self_referential_model(self):
|
|
|
|
|
class Person(models.Model):
|
2016-02-12 16:36:46 +00:00
|
|
|
|
friends = models.ManyToManyField(
|
|
|
|
|
"self", through="InvalidRelationship", symmetrical=False
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
class InvalidRelationship(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
first = models.ForeignKey(
|
|
|
|
|
Person, models.CASCADE, related_name="rel_from_set_2"
|
|
|
|
|
)
|
|
|
|
|
second = models.ForeignKey(
|
|
|
|
|
Person, models.CASCADE, related_name="rel_to_set_2"
|
|
|
|
|
)
|
|
|
|
|
third = models.ForeignKey(
|
|
|
|
|
Person, models.CASCADE, related_name="too_many_by_far"
|
2022-02-03 19:24:19 +00:00
|
|
|
|
)
|
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
field = Person._meta.get_field("friends")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Person),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2016-02-12 16:36:46 +00:00
|
|
|
|
"The model is used as an intermediate model by "
|
|
|
|
|
"'invalid_models_tests.Person.friends', but it has more than two "
|
|
|
|
|
"foreign keys to 'Person', which is ambiguous. You must specify "
|
|
|
|
|
"which two foreign keys Django should use via the through_fields "
|
|
|
|
|
"keyword argument.",
|
2014-02-19 18:01:55 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Use through_fields to specify which two foreign keys Django "
|
|
|
|
|
"should use."
|
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=InvalidRelationship,
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E333",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_foreign_key_to_abstract_model(self):
|
|
|
|
|
class AbstractModel(models.Model):
|
|
|
|
|
class Meta:
|
|
|
|
|
abstract = True
|
|
|
|
|
|
2015-10-05 20:29:23 +00:00
|
|
|
|
class Model(models.Model):
|
|
|
|
|
rel_string_foreign_key = models.ForeignKey("AbstractModel", models.CASCADE)
|
|
|
|
|
rel_class_foreign_key = models.ForeignKey(AbstractModel, models.CASCADE)
|
|
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
|
Model._meta.get_field("rel_string_foreign_key"),
|
|
|
|
|
Model._meta.get_field("rel_class_foreign_key"),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
]
|
2015-10-05 20:29:23 +00:00
|
|
|
|
expected_error = Error(
|
|
|
|
|
"Field defines a relation with model 'AbstractModel', "
|
|
|
|
|
"which is either not installed, or is abstract.",
|
|
|
|
|
id="fields.E300",
|
|
|
|
|
)
|
|
|
|
|
for field in fields:
|
|
|
|
|
expected_error.obj = field
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(field.check(), [expected_error])
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_m2m_to_abstract_model(self):
|
|
|
|
|
class AbstractModel(models.Model):
|
|
|
|
|
class Meta:
|
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
2015-10-05 20:29:23 +00:00
|
|
|
|
rel_string_m2m = models.ManyToManyField("AbstractModel")
|
|
|
|
|
rel_class_m2m = models.ManyToManyField(AbstractModel)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2015-10-05 20:29:23 +00:00
|
|
|
|
fields = [
|
|
|
|
|
Model._meta.get_field("rel_string_m2m"),
|
|
|
|
|
Model._meta.get_field("rel_class_m2m"),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
]
|
2015-10-05 20:29:23 +00:00
|
|
|
|
expected_error = Error(
|
|
|
|
|
"Field defines a relation with model 'AbstractModel', "
|
|
|
|
|
"which is either not installed, or is abstract.",
|
|
|
|
|
id="fields.E300",
|
|
|
|
|
)
|
|
|
|
|
for field in fields:
|
|
|
|
|
expected_error.obj = field
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(field.check(from_model=Model), [expected_error])
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_unique_m2m(self):
|
|
|
|
|
class Person(models.Model):
|
|
|
|
|
name = models.CharField(max_length=5)
|
|
|
|
|
|
|
|
|
|
class Group(models.Model):
|
|
|
|
|
members = models.ManyToManyField("Person", unique=True)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
field = Group._meta.get_field("members")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Group),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
|
"ManyToManyFields cannot be unique.",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E330",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_foreign_key_to_non_unique_field(self):
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
bad = models.IntegerField() # No unique=True
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
foreign_key = models.ForeignKey("Target", models.CASCADE, to_field="bad")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
field = Model._meta.get_field("foreign_key")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-02-19 21:57:16 +00:00
|
|
|
|
"'Target.bad' must be unique because it is referenced by a foreign "
|
|
|
|
|
"key.",
|
|
|
|
|
hint=(
|
|
|
|
|
"Add unique=True to this field or add a UniqueConstraint "
|
|
|
|
|
"(without condition) in the model Meta.constraints."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2020-02-19 21:57:16 +00:00
|
|
|
|
obj=field,
|
|
|
|
|
id="fields.E311",
|
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_foreign_key_to_non_unique_field_under_explicit_model(self):
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
bad = models.IntegerField()
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
field = models.ForeignKey(Target, models.CASCADE, to_field="bad")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
field = Model._meta.get_field("field")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-02-19 21:57:16 +00:00
|
|
|
|
"'Target.bad' must be unique because it is referenced by a foreign "
|
|
|
|
|
"key.",
|
|
|
|
|
hint=(
|
|
|
|
|
"Add unique=True to this field or add a UniqueConstraint "
|
|
|
|
|
"(without condition) in the model Meta.constraints."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2020-02-19 21:57:16 +00:00
|
|
|
|
id="fields.E311",
|
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2020-02-19 21:57:16 +00:00
|
|
|
|
def test_foreign_key_to_partially_unique_field(self):
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
source = models.IntegerField()
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
constraints = [
|
|
|
|
|
models.UniqueConstraint(
|
|
|
|
|
fields=["source"],
|
|
|
|
|
name="tfktpuf_partial_unique",
|
|
|
|
|
condition=models.Q(pk__gt=2),
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
field = models.ForeignKey(Target, models.CASCADE, to_field="source")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2020-02-19 21:57:16 +00:00
|
|
|
|
field = Model._meta.get_field("field")
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(),
|
|
|
|
|
[
|
|
|
|
|
Error(
|
|
|
|
|
"'Target.source' must be unique because it is referenced by a "
|
|
|
|
|
"foreign key.",
|
|
|
|
|
hint=(
|
|
|
|
|
"Add unique=True to this field or add a UniqueConstraint "
|
|
|
|
|
"(without condition) in the model Meta.constraints."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2020-02-19 21:57:16 +00:00
|
|
|
|
id="fields.E311",
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_foreign_key_to_unique_field_with_meta_constraint(self):
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
source = models.IntegerField()
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
constraints = [
|
|
|
|
|
models.UniqueConstraint(
|
|
|
|
|
fields=["source"],
|
|
|
|
|
name="tfktufwmc_unique",
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
field = models.ForeignKey(Target, models.CASCADE, to_field="source")
|
|
|
|
|
|
|
|
|
|
field = Model._meta.get_field("field")
|
|
|
|
|
self.assertEqual(field.check(), [])
|
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
def test_foreign_object_to_non_unique_fields(self):
|
|
|
|
|
class Person(models.Model):
|
|
|
|
|
# Note that both fields are not unique.
|
|
|
|
|
country_id = models.IntegerField()
|
|
|
|
|
city_id = models.IntegerField()
|
|
|
|
|
|
|
|
|
|
class MMembership(models.Model):
|
|
|
|
|
person_country_id = models.IntegerField()
|
|
|
|
|
person_city_id = models.IntegerField()
|
|
|
|
|
|
2016-02-12 16:36:46 +00:00
|
|
|
|
person = models.ForeignObject(
|
|
|
|
|
Person,
|
2015-07-22 14:43:21 +00:00
|
|
|
|
on_delete=models.CASCADE,
|
2014-01-20 02:45:21 +00:00
|
|
|
|
from_fields=["person_country_id", "person_city_id"],
|
2016-02-12 16:36:46 +00:00
|
|
|
|
to_fields=["country_id", "city_id"],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
field = MMembership._meta.get_field("person")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2015-10-13 13:08:27 +00:00
|
|
|
|
"No subset of the fields 'country_id', 'city_id' on model 'Person' "
|
|
|
|
|
"is unique.",
|
|
|
|
|
hint=(
|
2020-02-19 21:57:16 +00:00
|
|
|
|
"Mark a single field as unique=True or add a set of "
|
|
|
|
|
"fields to a unique constraint (via unique_together or a "
|
|
|
|
|
"UniqueConstraint (without condition) in the model "
|
|
|
|
|
"Meta.constraints)."
|
2015-10-13 13:08:27 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E310",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
)
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2020-02-19 21:57:16 +00:00
|
|
|
|
def test_foreign_object_to_partially_unique_field(self):
|
|
|
|
|
class Person(models.Model):
|
|
|
|
|
country_id = models.IntegerField()
|
|
|
|
|
city_id = models.IntegerField()
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
constraints = [
|
|
|
|
|
models.UniqueConstraint(
|
|
|
|
|
fields=["country_id", "city_id"],
|
|
|
|
|
name="tfotpuf_partial_unique",
|
|
|
|
|
condition=models.Q(pk__gt=2),
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
class MMembership(models.Model):
|
|
|
|
|
person_country_id = models.IntegerField()
|
|
|
|
|
person_city_id = models.IntegerField()
|
|
|
|
|
person = models.ForeignObject(
|
|
|
|
|
Person,
|
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
|
from_fields=["person_country_id", "person_city_id"],
|
|
|
|
|
to_fields=["country_id", "city_id"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
field = MMembership._meta.get_field("person")
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(),
|
|
|
|
|
[
|
|
|
|
|
Error(
|
|
|
|
|
"No subset of the fields 'country_id', 'city_id' on model "
|
|
|
|
|
"'Person' is unique.",
|
|
|
|
|
hint=(
|
|
|
|
|
"Mark a single field as unique=True or add a set of "
|
|
|
|
|
"fields to a unique constraint (via unique_together or a "
|
|
|
|
|
"UniqueConstraint (without condition) in the model "
|
|
|
|
|
"Meta.constraints)."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2020-02-19 21:57:16 +00:00
|
|
|
|
id="fields.E310",
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_foreign_object_to_unique_field_with_meta_constraint(self):
|
|
|
|
|
class Person(models.Model):
|
|
|
|
|
country_id = models.IntegerField()
|
|
|
|
|
city_id = models.IntegerField()
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
constraints = [
|
|
|
|
|
models.UniqueConstraint(
|
|
|
|
|
fields=["country_id", "city_id"],
|
|
|
|
|
name="tfotpuf_unique",
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
class MMembership(models.Model):
|
|
|
|
|
person_country_id = models.IntegerField()
|
|
|
|
|
person_city_id = models.IntegerField()
|
|
|
|
|
person = models.ForeignObject(
|
|
|
|
|
Person,
|
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
|
from_fields=["person_country_id", "person_city_id"],
|
|
|
|
|
to_fields=["country_id", "city_id"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
field = MMembership._meta.get_field("person")
|
|
|
|
|
self.assertEqual(field.check(), [])
|
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
def test_on_delete_set_null_on_non_nullable_field(self):
|
|
|
|
|
class Person(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
foreign_key = models.ForeignKey("Person", models.SET_NULL)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
field = Model._meta.get_field("foreign_key")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
|
"Field specifies on_delete=SET_NULL, but cannot be null.",
|
|
|
|
|
hint=(
|
|
|
|
|
"Set null=True argument on the field, or change the on_delete "
|
|
|
|
|
"rule."
|
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E320",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_on_delete_set_default_without_default_value(self):
|
|
|
|
|
class Person(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
foreign_key = models.ForeignKey("Person", models.SET_DEFAULT)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
field = Model._meta.get_field("foreign_key")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
|
"Field specifies on_delete=SET_DEFAULT, but has no default value.",
|
|
|
|
|
hint="Set a default value, or change the on_delete rule.",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E321",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_nullable_primary_key(self):
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
field = models.IntegerField(primary_key=True, null=True)
|
|
|
|
|
|
|
|
|
|
field = Model._meta.get_field("field")
|
2019-01-12 21:14:54 +00:00
|
|
|
|
with mock.patch.object(
|
|
|
|
|
connection.features, "interprets_empty_strings_as_nulls", False
|
|
|
|
|
):
|
|
|
|
|
results = field.check()
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
results,
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
|
|
|
|
"Primary keys must not have null=True.",
|
2014-03-03 10:18:39 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Set null=False on the field, or remove primary_key=True "
|
|
|
|
|
"argument."
|
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E007",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_not_swapped_model(self):
|
|
|
|
|
class SwappableModel(models.Model):
|
|
|
|
|
# A model that can be, but isn't swapped out. References to this
|
|
|
|
|
# model should *not* raise any validation error.
|
|
|
|
|
class Meta:
|
|
|
|
|
swappable = "TEST_SWAPPABLE_MODEL"
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
2016-02-12 16:36:46 +00:00
|
|
|
|
explicit_fk = models.ForeignKey(
|
|
|
|
|
SwappableModel,
|
2015-07-22 14:43:21 +00:00
|
|
|
|
models.CASCADE,
|
2016-02-12 16:36:46 +00:00
|
|
|
|
related_name="explicit_fk",
|
|
|
|
|
)
|
|
|
|
|
implicit_fk = models.ForeignKey(
|
|
|
|
|
"invalid_models_tests.SwappableModel",
|
2015-07-22 14:43:21 +00:00
|
|
|
|
models.CASCADE,
|
2016-02-12 16:36:46 +00:00
|
|
|
|
related_name="implicit_fk",
|
2022-02-03 19:24:19 +00:00
|
|
|
|
)
|
2016-02-12 16:36:46 +00:00
|
|
|
|
explicit_m2m = models.ManyToManyField(
|
|
|
|
|
SwappableModel, related_name="explicit_m2m"
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
implicit_m2m = models.ManyToManyField(
|
|
|
|
|
"invalid_models_tests.SwappableModel",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
related_name="implicit_m2m",
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
explicit_fk = Model._meta.get_field("explicit_fk")
|
|
|
|
|
self.assertEqual(explicit_fk.check(), [])
|
|
|
|
|
|
|
|
|
|
implicit_fk = Model._meta.get_field("implicit_fk")
|
|
|
|
|
self.assertEqual(implicit_fk.check(), [])
|
|
|
|
|
|
|
|
|
|
explicit_m2m = Model._meta.get_field("explicit_m2m")
|
|
|
|
|
self.assertEqual(explicit_m2m.check(from_model=Model), [])
|
|
|
|
|
|
|
|
|
|
implicit_m2m = Model._meta.get_field("implicit_m2m")
|
|
|
|
|
self.assertEqual(implicit_m2m.check(from_model=Model), [])
|
|
|
|
|
|
|
|
|
|
@override_settings(TEST_SWAPPED_MODEL="invalid_models_tests.Replacement")
|
|
|
|
|
def test_referencing_to_swapped_model(self):
|
|
|
|
|
class Replacement(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class SwappedModel(models.Model):
|
|
|
|
|
class Meta:
|
|
|
|
|
swappable = "TEST_SWAPPED_MODEL"
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
2016-02-12 16:36:46 +00:00
|
|
|
|
explicit_fk = models.ForeignKey(
|
|
|
|
|
SwappedModel,
|
2015-07-22 14:43:21 +00:00
|
|
|
|
models.CASCADE,
|
2016-02-12 16:36:46 +00:00
|
|
|
|
related_name="explicit_fk",
|
|
|
|
|
)
|
|
|
|
|
implicit_fk = models.ForeignKey(
|
|
|
|
|
"invalid_models_tests.SwappedModel",
|
2015-07-22 14:43:21 +00:00
|
|
|
|
models.CASCADE,
|
2016-02-12 16:36:46 +00:00
|
|
|
|
related_name="implicit_fk",
|
2022-02-03 19:24:19 +00:00
|
|
|
|
)
|
2016-02-12 16:36:46 +00:00
|
|
|
|
explicit_m2m = models.ManyToManyField(
|
|
|
|
|
SwappedModel, related_name="explicit_m2m"
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
implicit_m2m = models.ManyToManyField(
|
|
|
|
|
"invalid_models_tests.SwappedModel",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
related_name="implicit_m2m",
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
|
Model._meta.get_field("explicit_fk"),
|
|
|
|
|
Model._meta.get_field("implicit_fk"),
|
|
|
|
|
Model._meta.get_field("explicit_m2m"),
|
|
|
|
|
Model._meta.get_field("implicit_m2m"),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
expected_error = Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
|
(
|
|
|
|
|
"Field defines a relation with the model "
|
|
|
|
|
"'invalid_models_tests.SwappedModel', which has been swapped out."
|
|
|
|
|
),
|
|
|
|
|
hint="Update the relation to point at 'settings.TEST_SWAPPED_MODEL'.",
|
|
|
|
|
id="fields.E301",
|
2014-01-20 02:45:21 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for field in fields:
|
|
|
|
|
expected_error.obj = field
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(field.check(from_model=Model), [expected_error])
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2014-10-04 23:47:26 +00:00
|
|
|
|
def test_related_field_has_invalid_related_name(self):
|
|
|
|
|
digit = 0
|
|
|
|
|
illegal_non_alphanumeric = "!"
|
|
|
|
|
whitespace = "\t"
|
|
|
|
|
|
|
|
|
|
invalid_related_names = [
|
|
|
|
|
"%s_begins_with_digit" % digit,
|
|
|
|
|
"%s_begins_with_illegal_non_alphanumeric" % illegal_non_alphanumeric,
|
|
|
|
|
"%s_begins_with_whitespace" % whitespace,
|
|
|
|
|
"contains_%s_illegal_non_alphanumeric" % illegal_non_alphanumeric,
|
|
|
|
|
"contains_%s_whitespace" % whitespace,
|
|
|
|
|
"ends_with_with_illegal_non_alphanumeric_%s" % illegal_non_alphanumeric,
|
|
|
|
|
"ends_with_whitespace_%s" % whitespace,
|
2015-06-23 06:08:12 +00:00
|
|
|
|
"with", # a Python keyword
|
|
|
|
|
"related_name\n",
|
2015-10-16 16:21:30 +00:00
|
|
|
|
"",
|
2016-12-01 10:38:01 +00:00
|
|
|
|
",", # non-ASCII
|
2014-10-04 23:47:26 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
class Parent(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
for invalid_related_name in invalid_related_names:
|
2017-01-19 14:48:01 +00:00
|
|
|
|
Child = type(
|
|
|
|
|
"Child%s" % invalid_related_name,
|
|
|
|
|
(models.Model,),
|
|
|
|
|
{
|
2015-07-22 14:43:21 +00:00
|
|
|
|
"parent": models.ForeignKey(
|
|
|
|
|
"Parent", models.CASCADE, related_name=invalid_related_name
|
|
|
|
|
),
|
2014-10-04 23:47:26 +00:00
|
|
|
|
"__module__": Parent.__module__,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
field = Child._meta.get_field("parent")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Child.check(),
|
|
|
|
|
[
|
2014-10-04 23:47:26 +00:00
|
|
|
|
Error(
|
2016-05-22 19:10:24 +00:00
|
|
|
|
"The name '%s' is invalid related_name for field Child%s.parent"
|
2014-10-04 23:47:26 +00:00
|
|
|
|
% (invalid_related_name, invalid_related_name),
|
|
|
|
|
hint=(
|
|
|
|
|
"Related name must be a valid Python identifier or end "
|
|
|
|
|
"with a '+'"
|
|
|
|
|
),
|
|
|
|
|
obj=field,
|
|
|
|
|
id="fields.E306",
|
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-10-04 23:47:26 +00:00
|
|
|
|
|
|
|
|
|
def test_related_field_has_valid_related_name(self):
|
|
|
|
|
lowercase = "a"
|
|
|
|
|
uppercase = "A"
|
|
|
|
|
digit = 0
|
|
|
|
|
|
|
|
|
|
related_names = [
|
|
|
|
|
"%s_starts_with_lowercase" % lowercase,
|
|
|
|
|
"%s_tarts_with_uppercase" % uppercase,
|
|
|
|
|
"_starts_with_underscore",
|
|
|
|
|
"contains_%s_digit" % digit,
|
|
|
|
|
"ends_with_plus+",
|
|
|
|
|
"_+",
|
|
|
|
|
"+",
|
2016-12-01 10:38:01 +00:00
|
|
|
|
"試",
|
|
|
|
|
"試驗+",
|
2014-10-04 23:47:26 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
class Parent(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
for related_name in related_names:
|
2017-01-19 14:48:01 +00:00
|
|
|
|
Child = type(
|
|
|
|
|
"Child%s" % related_name,
|
|
|
|
|
(models.Model,),
|
|
|
|
|
{
|
2015-07-22 14:43:21 +00:00
|
|
|
|
"parent": models.ForeignKey(
|
|
|
|
|
"Parent", models.CASCADE, related_name=related_name
|
|
|
|
|
),
|
2014-10-04 23:47:26 +00:00
|
|
|
|
"__module__": Parent.__module__,
|
|
|
|
|
},
|
|
|
|
|
)
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(Child.check(), [])
|
2014-10-04 23:47:26 +00:00
|
|
|
|
|
2016-06-07 08:55:27 +00:00
|
|
|
|
def test_to_fields_exist(self):
|
|
|
|
|
class Parent(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Child(models.Model):
|
|
|
|
|
a = models.PositiveIntegerField()
|
|
|
|
|
b = models.PositiveIntegerField()
|
2019-08-20 07:54:41 +00:00
|
|
|
|
parent = models.ForeignObject(
|
2016-06-07 08:55:27 +00:00
|
|
|
|
Parent,
|
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
|
from_fields=("a", "b"),
|
|
|
|
|
to_fields=("a", "b"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
field = Child._meta.get_field("parent")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(),
|
|
|
|
|
[
|
2016-06-07 08:55:27 +00:00
|
|
|
|
Error(
|
|
|
|
|
"The to_field 'a' doesn't exist on the related model "
|
|
|
|
|
"'invalid_models_tests.Parent'.",
|
|
|
|
|
obj=field,
|
|
|
|
|
id="fields.E312",
|
|
|
|
|
),
|
|
|
|
|
Error(
|
|
|
|
|
"The to_field 'b' doesn't exist on the related model "
|
|
|
|
|
"'invalid_models_tests.Parent'.",
|
|
|
|
|
obj=field,
|
|
|
|
|
id="fields.E312",
|
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2016-06-07 08:55:27 +00:00
|
|
|
|
|
|
|
|
|
def test_to_fields_not_checked_if_related_model_doesnt_exist(self):
|
|
|
|
|
class Child(models.Model):
|
|
|
|
|
a = models.PositiveIntegerField()
|
|
|
|
|
b = models.PositiveIntegerField()
|
2019-08-20 07:54:41 +00:00
|
|
|
|
parent = models.ForeignObject(
|
2016-06-07 08:55:27 +00:00
|
|
|
|
"invalid_models_tests.Parent",
|
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
|
from_fields=("a", "b"),
|
|
|
|
|
to_fields=("a", "b"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
field = Child._meta.get_field("parent")
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(),
|
|
|
|
|
[
|
|
|
|
|
Error(
|
|
|
|
|
"Field defines a relation with model "
|
|
|
|
|
"'invalid_models_tests.Parent', which is either not installed, or "
|
|
|
|
|
"is abstract.",
|
|
|
|
|
id="fields.E300",
|
|
|
|
|
obj=field,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
2016-05-22 19:10:24 +00:00
|
|
|
|
def test_invalid_related_query_name(self):
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
first = models.ForeignKey(
|
|
|
|
|
Target, models.CASCADE, related_name="contains__double"
|
|
|
|
|
)
|
|
|
|
|
second = models.ForeignKey(
|
|
|
|
|
Target, models.CASCADE, related_query_name="ends_underscore_"
|
|
|
|
|
)
|
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
2022-02-03 19:24:19 +00:00
|
|
|
|
[
|
|
|
|
|
Error(
|
2016-05-22 19:10:24 +00:00
|
|
|
|
"Reverse query name 'contains__double' must not contain '__'.",
|
2022-02-03 19:24:19 +00:00
|
|
|
|
hint=(
|
2016-05-22 19:10:24 +00:00
|
|
|
|
"Add or change a related_name or related_query_name "
|
|
|
|
|
"argument for this field."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2016-05-22 19:10:24 +00:00
|
|
|
|
obj=Model._meta.get_field("first"),
|
|
|
|
|
id="fields.E309",
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
|
|
|
|
Error(
|
2016-05-22 19:10:24 +00:00
|
|
|
|
"Reverse query name 'ends_underscore_' must not end with an "
|
|
|
|
|
"underscore.",
|
2022-02-03 19:24:19 +00:00
|
|
|
|
hint=(
|
2016-05-22 19:10:24 +00:00
|
|
|
|
"Add or change a related_name or related_query_name "
|
|
|
|
|
"argument for this field."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2016-05-22 19:10:24 +00:00
|
|
|
|
obj=Model._meta.get_field("second"),
|
|
|
|
|
id="fields.E308",
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
class AccessorClashTests(SimpleTestCase):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
def test_fk_to_integer(self):
|
|
|
|
|
self._test_accessor_clash(
|
|
|
|
|
target=models.IntegerField(),
|
2015-07-22 14:43:21 +00:00
|
|
|
|
relative=models.ForeignKey("Target", models.CASCADE),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_fk_to_fk(self):
|
|
|
|
|
self._test_accessor_clash(
|
2015-07-22 14:43:21 +00:00
|
|
|
|
target=models.ForeignKey("Another", models.CASCADE),
|
|
|
|
|
relative=models.ForeignKey("Target", models.CASCADE),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_fk_to_m2m(self):
|
|
|
|
|
self._test_accessor_clash(
|
|
|
|
|
target=models.ManyToManyField("Another"),
|
2015-07-22 14:43:21 +00:00
|
|
|
|
relative=models.ForeignKey("Target", models.CASCADE),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_m2m_to_integer(self):
|
|
|
|
|
self._test_accessor_clash(
|
|
|
|
|
target=models.IntegerField(), relative=models.ManyToManyField("Target")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_m2m_to_fk(self):
|
|
|
|
|
self._test_accessor_clash(
|
2015-07-22 14:43:21 +00:00
|
|
|
|
target=models.ForeignKey("Another", models.CASCADE),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
relative=models.ManyToManyField("Target"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_m2m_to_m2m(self):
|
|
|
|
|
self._test_accessor_clash(
|
|
|
|
|
target=models.ManyToManyField("Another"),
|
|
|
|
|
relative=models.ManyToManyField("Target"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _test_accessor_clash(self, target, relative):
|
|
|
|
|
class Another(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
model_set = target
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
rel = relative
|
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Target.model_set' for "
|
|
|
|
|
"'invalid_models_tests.Model.rel' clashes with field name "
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"'invalid_models_tests.Target.model_set'.",
|
|
|
|
|
hint=(
|
|
|
|
|
"Rename field 'invalid_models_tests.Target.model_set', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.rel'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("rel"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E302",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_clash_between_accessors(self):
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
foreign = models.ForeignKey(Target, models.CASCADE)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
m2m = models.ManyToManyField(Target)
|
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Target.model_set' for "
|
|
|
|
|
"'invalid_models_tests.Model.foreign' clashes with reverse "
|
|
|
|
|
"accessor for 'invalid_models_tests.Model.m2m'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"for 'invalid_models_tests.Model.foreign' or "
|
|
|
|
|
"'invalid_models_tests.Model.m2m'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("foreign"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E304",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Target.model_set' for "
|
|
|
|
|
"'invalid_models_tests.Model.m2m' clashes with reverse "
|
|
|
|
|
"accessor for 'invalid_models_tests.Model.foreign'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"for 'invalid_models_tests.Model.m2m' or "
|
|
|
|
|
"'invalid_models_tests.Model.foreign'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("m2m"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E304",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2014-02-15 14:36:07 +00:00
|
|
|
|
def test_m2m_to_m2m_with_inheritance(self):
|
|
|
|
|
"""Ref #22047."""
|
|
|
|
|
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
2016-04-08 02:04:45 +00:00
|
|
|
|
children = models.ManyToManyField(
|
|
|
|
|
"Child", related_name="m2m_clash", related_query_name="no_clash"
|
|
|
|
|
)
|
2014-02-15 14:36:07 +00:00
|
|
|
|
|
|
|
|
|
class Parent(models.Model):
|
|
|
|
|
m2m_clash = models.ManyToManyField("Target")
|
|
|
|
|
|
|
|
|
|
class Child(Parent):
|
|
|
|
|
pass
|
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-02-15 14:36:07 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Child.m2m_clash' for "
|
|
|
|
|
"'invalid_models_tests.Model.children' clashes with field "
|
|
|
|
|
"name 'invalid_models_tests.Child.m2m_clash'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Child.m2m_clash', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.children'."
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-02-15 14:36:07 +00:00
|
|
|
|
obj=Model._meta.get_field("children"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E302",
|
2014-02-15 14:36:07 +00:00
|
|
|
|
)
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-02-15 14:36:07 +00:00
|
|
|
|
|
2016-08-16 18:23:30 +00:00
|
|
|
|
def test_no_clash_for_hidden_related_name(self):
|
|
|
|
|
class Stub(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class ManyToManyRel(models.Model):
|
|
|
|
|
thing1 = models.ManyToManyField(Stub, related_name="+")
|
|
|
|
|
thing2 = models.ManyToManyField(Stub, related_name="+")
|
|
|
|
|
|
|
|
|
|
class FKRel(models.Model):
|
|
|
|
|
thing1 = models.ForeignKey(Stub, models.CASCADE, related_name="+")
|
|
|
|
|
thing2 = models.ForeignKey(Stub, models.CASCADE, related_name="+")
|
|
|
|
|
|
|
|
|
|
self.assertEqual(ManyToManyRel.check(), [])
|
|
|
|
|
self.assertEqual(FKRel.check(), [])
|
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
class ReverseQueryNameClashTests(SimpleTestCase):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
def test_fk_to_integer(self):
|
|
|
|
|
self._test_reverse_query_name_clash(
|
|
|
|
|
target=models.IntegerField(),
|
2015-07-22 14:43:21 +00:00
|
|
|
|
relative=models.ForeignKey("Target", models.CASCADE),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_fk_to_fk(self):
|
|
|
|
|
self._test_reverse_query_name_clash(
|
2015-07-22 14:43:21 +00:00
|
|
|
|
target=models.ForeignKey("Another", models.CASCADE),
|
|
|
|
|
relative=models.ForeignKey("Target", models.CASCADE),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_fk_to_m2m(self):
|
|
|
|
|
self._test_reverse_query_name_clash(
|
|
|
|
|
target=models.ManyToManyField("Another"),
|
2015-07-22 14:43:21 +00:00
|
|
|
|
relative=models.ForeignKey("Target", models.CASCADE),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_m2m_to_integer(self):
|
|
|
|
|
self._test_reverse_query_name_clash(
|
|
|
|
|
target=models.IntegerField(), relative=models.ManyToManyField("Target")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_m2m_to_fk(self):
|
|
|
|
|
self._test_reverse_query_name_clash(
|
2015-07-22 14:43:21 +00:00
|
|
|
|
target=models.ForeignKey("Another", models.CASCADE),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
relative=models.ManyToManyField("Target"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_m2m_to_m2m(self):
|
|
|
|
|
self._test_reverse_query_name_clash(
|
|
|
|
|
target=models.ManyToManyField("Another"),
|
|
|
|
|
relative=models.ManyToManyField("Target"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _test_reverse_query_name_clash(self, target, relative):
|
|
|
|
|
class Another(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
model = target
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
rel = relative
|
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.rel' "
|
|
|
|
|
"clashes with field name 'invalid_models_tests.Target.model'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Target.model', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.rel'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("rel"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E303",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2020-12-30 02:22:17 +00:00
|
|
|
|
@modify_settings(INSTALLED_APPS={"append": "basic"})
|
|
|
|
|
@isolate_apps("basic", "invalid_models_tests")
|
|
|
|
|
def test_no_clash_across_apps_without_accessor(self):
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
class Meta:
|
|
|
|
|
app_label = "invalid_models_tests"
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
m2m = models.ManyToManyField(Target, related_name="+")
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
app_label = "basic"
|
|
|
|
|
|
|
|
|
|
def _test():
|
|
|
|
|
# Define model with the same name.
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
m2m = models.ManyToManyField(Target, related_name="+")
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
app_label = "invalid_models_tests"
|
|
|
|
|
|
|
|
|
|
self.assertEqual(Model.check(), [])
|
|
|
|
|
|
|
|
|
|
_test()
|
|
|
|
|
self.assertEqual(Model.check(), [])
|
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
class ExplicitRelatedNameClashTests(SimpleTestCase):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
def test_fk_to_integer(self):
|
|
|
|
|
self._test_explicit_related_name_clash(
|
|
|
|
|
target=models.IntegerField(),
|
2015-07-22 14:43:21 +00:00
|
|
|
|
relative=models.ForeignKey("Target", models.CASCADE, related_name="clash"),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_fk_to_fk(self):
|
|
|
|
|
self._test_explicit_related_name_clash(
|
2015-07-22 14:43:21 +00:00
|
|
|
|
target=models.ForeignKey("Another", models.CASCADE),
|
|
|
|
|
relative=models.ForeignKey("Target", models.CASCADE, related_name="clash"),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_fk_to_m2m(self):
|
|
|
|
|
self._test_explicit_related_name_clash(
|
|
|
|
|
target=models.ManyToManyField("Another"),
|
2015-07-22 14:43:21 +00:00
|
|
|
|
relative=models.ForeignKey("Target", models.CASCADE, related_name="clash"),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_m2m_to_integer(self):
|
|
|
|
|
self._test_explicit_related_name_clash(
|
|
|
|
|
target=models.IntegerField(),
|
|
|
|
|
relative=models.ManyToManyField("Target", related_name="clash"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_m2m_to_fk(self):
|
|
|
|
|
self._test_explicit_related_name_clash(
|
2015-07-22 14:43:21 +00:00
|
|
|
|
target=models.ForeignKey("Another", models.CASCADE),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
relative=models.ManyToManyField("Target", related_name="clash"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_m2m_to_m2m(self):
|
|
|
|
|
self._test_explicit_related_name_clash(
|
|
|
|
|
target=models.ManyToManyField("Another"),
|
|
|
|
|
relative=models.ManyToManyField("Target", related_name="clash"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _test_explicit_related_name_clash(self, target, relative):
|
|
|
|
|
class Another(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
clash = target
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
rel = relative
|
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Target.clash' for "
|
|
|
|
|
"'invalid_models_tests.Model.rel' clashes with field name "
|
|
|
|
|
"'invalid_models_tests.Target.clash'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Target.clash', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.rel'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("rel"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E302",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.rel' "
|
|
|
|
|
"clashes with field name 'invalid_models_tests.Target.clash'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Target.clash', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.rel'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("rel"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E303",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
class ExplicitRelatedQueryNameClashTests(SimpleTestCase):
|
2016-02-07 23:05:14 +00:00
|
|
|
|
def test_fk_to_integer(self, related_name=None):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
self._test_explicit_related_query_name_clash(
|
|
|
|
|
target=models.IntegerField(),
|
2016-04-08 02:04:45 +00:00
|
|
|
|
relative=models.ForeignKey(
|
|
|
|
|
"Target",
|
2015-07-22 14:43:21 +00:00
|
|
|
|
models.CASCADE,
|
2016-02-07 23:05:14 +00:00
|
|
|
|
related_name=related_name,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
related_query_name="clash",
|
|
|
|
|
),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2016-02-07 23:05:14 +00:00
|
|
|
|
def test_hidden_fk_to_integer(self, related_name=None):
|
|
|
|
|
self.test_fk_to_integer(related_name="+")
|
|
|
|
|
|
|
|
|
|
def test_fk_to_fk(self, related_name=None):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
self._test_explicit_related_query_name_clash(
|
2015-07-22 14:43:21 +00:00
|
|
|
|
target=models.ForeignKey("Another", models.CASCADE),
|
2016-04-08 02:04:45 +00:00
|
|
|
|
relative=models.ForeignKey(
|
|
|
|
|
"Target",
|
2015-07-22 14:43:21 +00:00
|
|
|
|
models.CASCADE,
|
2016-02-07 23:05:14 +00:00
|
|
|
|
related_name=related_name,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
related_query_name="clash",
|
|
|
|
|
),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2016-02-07 23:05:14 +00:00
|
|
|
|
def test_hidden_fk_to_fk(self):
|
|
|
|
|
self.test_fk_to_fk(related_name="+")
|
|
|
|
|
|
|
|
|
|
def test_fk_to_m2m(self, related_name=None):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
self._test_explicit_related_query_name_clash(
|
|
|
|
|
target=models.ManyToManyField("Another"),
|
2016-04-08 02:04:45 +00:00
|
|
|
|
relative=models.ForeignKey(
|
|
|
|
|
"Target",
|
2015-07-22 14:43:21 +00:00
|
|
|
|
models.CASCADE,
|
2016-02-07 23:05:14 +00:00
|
|
|
|
related_name=related_name,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
related_query_name="clash",
|
|
|
|
|
),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2016-02-07 23:05:14 +00:00
|
|
|
|
def test_hidden_fk_to_m2m(self):
|
|
|
|
|
self.test_fk_to_m2m(related_name="+")
|
|
|
|
|
|
|
|
|
|
def test_m2m_to_integer(self, related_name=None):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
self._test_explicit_related_query_name_clash(
|
|
|
|
|
target=models.IntegerField(),
|
2016-04-08 02:04:45 +00:00
|
|
|
|
relative=models.ManyToManyField(
|
|
|
|
|
"Target", related_name=related_name, related_query_name="clash"
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2016-04-08 02:04:45 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2016-02-07 23:05:14 +00:00
|
|
|
|
def test_hidden_m2m_to_integer(self):
|
|
|
|
|
self.test_m2m_to_integer(related_name="+")
|
|
|
|
|
|
|
|
|
|
def test_m2m_to_fk(self, related_name=None):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
self._test_explicit_related_query_name_clash(
|
2015-07-22 14:43:21 +00:00
|
|
|
|
target=models.ForeignKey("Another", models.CASCADE),
|
2016-04-08 02:04:45 +00:00
|
|
|
|
relative=models.ManyToManyField(
|
|
|
|
|
"Target", related_name=related_name, related_query_name="clash"
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2016-04-08 02:04:45 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2016-02-07 23:05:14 +00:00
|
|
|
|
def test_hidden_m2m_to_fk(self):
|
|
|
|
|
self.test_m2m_to_fk(related_name="+")
|
|
|
|
|
|
|
|
|
|
def test_m2m_to_m2m(self, related_name=None):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
self._test_explicit_related_query_name_clash(
|
|
|
|
|
target=models.ManyToManyField("Another"),
|
2016-04-08 02:04:45 +00:00
|
|
|
|
relative=models.ManyToManyField(
|
|
|
|
|
"Target",
|
2016-02-07 23:05:14 +00:00
|
|
|
|
related_name=related_name,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
related_query_name="clash",
|
|
|
|
|
),
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2016-02-07 23:05:14 +00:00
|
|
|
|
def test_hidden_m2m_to_m2m(self):
|
|
|
|
|
self.test_m2m_to_m2m(related_name="+")
|
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
def _test_explicit_related_query_name_clash(self, target, relative):
|
|
|
|
|
class Another(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
clash = target
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
rel = relative
|
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.rel' "
|
|
|
|
|
"clashes with field name 'invalid_models_tests.Target.clash'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Target.clash', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.rel'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("rel"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E303",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
class SelfReferentialM2MClashTests(SimpleTestCase):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
def test_clash_between_accessors(self):
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
first_m2m = models.ManyToManyField("self", symmetrical=False)
|
|
|
|
|
second_m2m = models.ManyToManyField("self", symmetrical=False)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Model.model_set' for "
|
|
|
|
|
"'invalid_models_tests.Model.first_m2m' clashes with reverse "
|
|
|
|
|
"accessor for 'invalid_models_tests.Model.second_m2m'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"for 'invalid_models_tests.Model.first_m2m' or "
|
|
|
|
|
"'invalid_models_tests.Model.second_m2m'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("first_m2m"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E304",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Model.model_set' for "
|
|
|
|
|
"'invalid_models_tests.Model.second_m2m' clashes with reverse "
|
|
|
|
|
"accessor for 'invalid_models_tests.Model.first_m2m'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"for 'invalid_models_tests.Model.second_m2m' or "
|
|
|
|
|
"'invalid_models_tests.Model.first_m2m'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("second_m2m"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E304",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_accessor_clash(self):
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
model_set = models.ManyToManyField("self", symmetrical=False)
|
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Model.model_set' for "
|
|
|
|
|
"'invalid_models_tests.Model.model_set' clashes with field "
|
|
|
|
|
"name 'invalid_models_tests.Model.model_set'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Model.model_set', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.model_set'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("model_set"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E302",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_reverse_query_name_clash(self):
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
model = models.ManyToManyField("self", symmetrical=False)
|
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.model' "
|
|
|
|
|
"clashes with field name 'invalid_models_tests.Model.model'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Model.model', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.model'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("model"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E303",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_clash_under_explicit_related_name(self):
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
clash = models.IntegerField()
|
2016-02-12 16:36:46 +00:00
|
|
|
|
m2m = models.ManyToManyField(
|
|
|
|
|
"self", symmetrical=False, related_name="clash"
|
|
|
|
|
)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Model.clash' for "
|
|
|
|
|
"'invalid_models_tests.Model.m2m' clashes with field name "
|
|
|
|
|
"'invalid_models_tests.Model.clash'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Model.clash', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.m2m'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("m2m"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E302",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.m2m' "
|
|
|
|
|
"clashes with field name 'invalid_models_tests.Model.clash'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Model.clash', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.m2m'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("m2m"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E303",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_valid_model(self):
|
|
|
|
|
class Model(models.Model):
|
2016-02-12 16:36:46 +00:00
|
|
|
|
first = models.ManyToManyField(
|
|
|
|
|
"self", symmetrical=False, related_name="first_accessor"
|
|
|
|
|
)
|
|
|
|
|
second = models.ManyToManyField(
|
|
|
|
|
"self", symmetrical=False, related_name="second_accessor"
|
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(Model.check(), [])
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
class SelfReferentialFKClashTests(SimpleTestCase):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
def test_accessor_clash(self):
|
|
|
|
|
class Model(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
model_set = models.ForeignKey("Model", models.CASCADE)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Model.model_set' for "
|
|
|
|
|
"'invalid_models_tests.Model.model_set' clashes with field "
|
|
|
|
|
"name 'invalid_models_tests.Model.model_set'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Model.model_set', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.model_set'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("model_set"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E302",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_reverse_query_name_clash(self):
|
|
|
|
|
class Model(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
model = models.ForeignKey("Model", models.CASCADE)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.model' "
|
|
|
|
|
"clashes with field name 'invalid_models_tests.Model.model'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Model.model', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.model'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("model"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E303",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
def test_clash_under_explicit_related_name(self):
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
clash = models.CharField(max_length=10)
|
2015-07-22 14:43:21 +00:00
|
|
|
|
foreign = models.ForeignKey("Model", models.CASCADE, related_name="clash")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Model.clash' for "
|
|
|
|
|
"'invalid_models_tests.Model.foreign' clashes with field name "
|
|
|
|
|
"'invalid_models_tests.Model.clash'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Model.clash', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.foreign'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("foreign"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E302",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.foreign' "
|
|
|
|
|
"clashes with field name 'invalid_models_tests.Model.clash'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Rename field 'invalid_models_tests.Model.clash', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.foreign'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("foreign"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E303",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
class ComplexClashTests(SimpleTestCase):
|
2014-01-20 02:45:21 +00:00
|
|
|
|
# New tests should not be included here, because this is a single,
|
|
|
|
|
# self-contained sanity check, not a test of everything.
|
|
|
|
|
def test_complex_clash(self):
|
|
|
|
|
class Target(models.Model):
|
|
|
|
|
tgt_safe = models.CharField(max_length=10)
|
|
|
|
|
clash = models.CharField(max_length=10)
|
|
|
|
|
model = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
|
|
clash1_set = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
|
src_safe = models.CharField(max_length=10)
|
|
|
|
|
|
2015-07-22 14:43:21 +00:00
|
|
|
|
foreign_1 = models.ForeignKey(Target, models.CASCADE, related_name="id")
|
|
|
|
|
foreign_2 = models.ForeignKey(
|
|
|
|
|
Target, models.CASCADE, related_name="src_safe"
|
|
|
|
|
)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
|
m2m_1 = models.ManyToManyField(Target, related_name="id")
|
|
|
|
|
m2m_2 = models.ManyToManyField(Target, related_name="src_safe")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
Model.check(),
|
|
|
|
|
[
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Target.id' for "
|
|
|
|
|
"'invalid_models_tests.Model.foreign_1' clashes with field "
|
|
|
|
|
"name 'invalid_models_tests.Target.id'.",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Rename field 'invalid_models_tests.Target.id', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.foreign_1'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("foreign_1"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E302",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.foreign_1' "
|
|
|
|
|
"clashes with field name 'invalid_models_tests.Target.id'.",
|
|
|
|
|
hint=(
|
|
|
|
|
"Rename field 'invalid_models_tests.Target.id', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.foreign_1'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("foreign_1"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E303",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Target.id' for "
|
|
|
|
|
"'invalid_models_tests.Model.foreign_1' clashes with reverse "
|
|
|
|
|
"accessor for 'invalid_models_tests.Model.m2m_1'.",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
|
|
|
|
"for 'invalid_models_tests.Model.foreign_1' or "
|
|
|
|
|
"'invalid_models_tests.Model.m2m_1'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("foreign_1"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E304",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.foreign_1' "
|
|
|
|
|
"clashes with reverse query name for "
|
|
|
|
|
"'invalid_models_tests.Model.m2m_1'.",
|
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
|
|
|
|
"for 'invalid_models_tests.Model.foreign_1' or "
|
|
|
|
|
"'invalid_models_tests.Model.m2m_1'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("foreign_1"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E305",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Target.src_safe' for "
|
|
|
|
|
"'invalid_models_tests.Model.foreign_2' clashes with reverse "
|
|
|
|
|
"accessor for 'invalid_models_tests.Model.m2m_2'.",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
|
|
|
|
"for 'invalid_models_tests.Model.foreign_2' or "
|
|
|
|
|
"'invalid_models_tests.Model.m2m_2'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("foreign_2"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E304",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.foreign_2' "
|
|
|
|
|
"clashes with reverse query name for "
|
|
|
|
|
"'invalid_models_tests.Model.m2m_2'.",
|
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
|
|
|
|
"for 'invalid_models_tests.Model.foreign_2' or "
|
|
|
|
|
"'invalid_models_tests.Model.m2m_2'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("foreign_2"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E305",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Target.id' for "
|
|
|
|
|
"'invalid_models_tests.Model.m2m_1' clashes with field name "
|
|
|
|
|
"'invalid_models_tests.Target.id'.",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Rename field 'invalid_models_tests.Target.id', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.m2m_1'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("m2m_1"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E302",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.m2m_1' "
|
|
|
|
|
"clashes with field name 'invalid_models_tests.Target.id'.",
|
|
|
|
|
hint=(
|
|
|
|
|
"Rename field 'invalid_models_tests.Target.id', or "
|
|
|
|
|
"add/change a related_name argument to the definition for "
|
|
|
|
|
"field 'invalid_models_tests.Model.m2m_1'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("m2m_1"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E303",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Target.id' for "
|
|
|
|
|
"'invalid_models_tests.Model.m2m_1' clashes with reverse "
|
|
|
|
|
"accessor for 'invalid_models_tests.Model.foreign_1'.",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
|
|
|
|
"for 'invalid_models_tests.Model.m2m_1' or "
|
|
|
|
|
"'invalid_models_tests.Model.foreign_1'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("m2m_1"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E304",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.m2m_1' "
|
|
|
|
|
"clashes with reverse query name for "
|
|
|
|
|
"'invalid_models_tests.Model.foreign_1'.",
|
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
|
|
|
|
"for 'invalid_models_tests.Model.m2m_1' or "
|
|
|
|
|
"'invalid_models_tests.Model.foreign_1'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("m2m_1"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E305",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse accessor 'Target.src_safe' for "
|
|
|
|
|
"'invalid_models_tests.Model.m2m_2' clashes with reverse "
|
|
|
|
|
"accessor for 'invalid_models_tests.Model.foreign_2'.",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
|
|
|
|
"for 'invalid_models_tests.Model.m2m_2' or "
|
|
|
|
|
"'invalid_models_tests.Model.foreign_2'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("m2m_2"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E304",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
Error(
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"Reverse query name for 'invalid_models_tests.Model.m2m_2' "
|
|
|
|
|
"clashes with reverse query name for "
|
|
|
|
|
"'invalid_models_tests.Model.foreign_2'.",
|
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
|
|
|
|
"for 'invalid_models_tests.Model.m2m_2' or "
|
|
|
|
|
"'invalid_models_tests.Model.foreign_2'."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2014-01-20 02:45:21 +00:00
|
|
|
|
obj=Model._meta.get_field("m2m_2"),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
id="fields.E305",
|
2020-12-28 11:48:07 +00:00
|
|
|
|
),
|
2014-03-03 10:18:39 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2014-02-19 18:01:55 +00:00
|
|
|
|
|
2020-01-16 07:06:16 +00:00
|
|
|
|
def test_clash_parent_link(self):
|
|
|
|
|
class Parent(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Child(Parent):
|
|
|
|
|
other_parent = models.OneToOneField(Parent, models.CASCADE)
|
|
|
|
|
|
|
|
|
|
errors = [
|
2021-09-22 05:53:10 +00:00
|
|
|
|
(
|
|
|
|
|
"fields.E304",
|
|
|
|
|
"accessor",
|
|
|
|
|
" 'Parent.child'",
|
|
|
|
|
"parent_ptr",
|
|
|
|
|
"other_parent",
|
|
|
|
|
),
|
|
|
|
|
("fields.E305", "query name", "", "parent_ptr", "other_parent"),
|
|
|
|
|
(
|
|
|
|
|
"fields.E304",
|
|
|
|
|
"accessor",
|
|
|
|
|
" 'Parent.child'",
|
|
|
|
|
"other_parent",
|
|
|
|
|
"parent_ptr",
|
|
|
|
|
),
|
|
|
|
|
("fields.E305", "query name", "", "other_parent", "parent_ptr"),
|
2020-01-16 07:06:16 +00:00
|
|
|
|
]
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
Child.check(),
|
|
|
|
|
[
|
|
|
|
|
Error(
|
2021-09-22 05:53:10 +00:00
|
|
|
|
"Reverse %s%s for 'invalid_models_tests.Child.%s' clashes with "
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"reverse %s for 'invalid_models_tests.Child.%s'."
|
2021-09-22 05:53:10 +00:00
|
|
|
|
% (attr, rel_name, field_name, attr, clash_name),
|
2020-01-16 07:06:16 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Add or change a related_name argument to the definition "
|
2020-12-28 11:48:07 +00:00
|
|
|
|
"for 'invalid_models_tests.Child.%s' or "
|
|
|
|
|
"'invalid_models_tests.Child.%s'." % (field_name, clash_name)
|
2020-01-16 07:06:16 +00:00
|
|
|
|
),
|
|
|
|
|
obj=Child._meta.get_field(field_name),
|
|
|
|
|
id=error_id,
|
|
|
|
|
)
|
2021-09-22 05:53:10 +00:00
|
|
|
|
for error_id, attr, rel_name, field_name, clash_name in errors
|
2020-01-16 07:06:16 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
2014-02-19 18:01:55 +00:00
|
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
|
class M2mThroughFieldsTests(SimpleTestCase):
|
2014-02-19 18:01:55 +00:00
|
|
|
|
def test_m2m_field_argument_validation(self):
|
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
|
ManyToManyField accepts the ``through_fields`` kwarg
|
2014-02-19 18:01:55 +00:00
|
|
|
|
only if an intermediary table is specified.
|
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-02-19 18:01:55 +00:00
|
|
|
|
class Fan(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
2016-01-04 08:50:08 +00:00
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
|
ValueError, "Cannot specify through_fields without a through model"
|
|
|
|
|
):
|
|
|
|
|
models.ManyToManyField(Fan, through_fields=("f1", "f2"))
|
2014-02-19 18:01:55 +00:00
|
|
|
|
|
2014-03-07 11:56:28 +00:00
|
|
|
|
def test_invalid_order(self):
|
2014-02-19 18:01:55 +00:00
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
|
Mixing up the order of link fields to ManyToManyField.through_fields
|
2014-03-07 11:56:28 +00:00
|
|
|
|
triggers validation errors.
|
2014-02-19 18:01:55 +00:00
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-02-19 18:01:55 +00:00
|
|
|
|
class Fan(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Event(models.Model):
|
2014-03-07 11:56:28 +00:00
|
|
|
|
invitees = models.ManyToManyField(
|
|
|
|
|
Fan, through="Invitation", through_fields=("invitee", "event")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
)
|
2014-02-19 18:01:55 +00:00
|
|
|
|
|
|
|
|
|
class Invitation(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
event = models.ForeignKey(Event, models.CASCADE)
|
|
|
|
|
invitee = models.ForeignKey(Fan, models.CASCADE)
|
|
|
|
|
inviter = models.ForeignKey(Fan, models.CASCADE, related_name="+")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-03-07 11:56:28 +00:00
|
|
|
|
field = Event._meta.get_field("invitees")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Event),
|
|
|
|
|
[
|
2014-03-07 11:56:28 +00:00
|
|
|
|
Error(
|
2016-02-12 16:36:46 +00:00
|
|
|
|
"'Invitation.invitee' is not a foreign key to 'Event'.",
|
2014-03-07 11:56:28 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Did you mean one of the following foreign keys to 'Event': "
|
|
|
|
|
"event?"
|
|
|
|
|
),
|
|
|
|
|
obj=field,
|
2016-02-12 16:36:46 +00:00
|
|
|
|
id="fields.E339",
|
|
|
|
|
),
|
2014-03-07 11:56:28 +00:00
|
|
|
|
Error(
|
2016-02-12 16:36:46 +00:00
|
|
|
|
"'Invitation.event' is not a foreign key to 'Fan'.",
|
2014-03-07 11:56:28 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Did you mean one of the following foreign keys to 'Fan': "
|
|
|
|
|
"invitee, inviter?"
|
|
|
|
|
),
|
|
|
|
|
obj=field,
|
2016-02-12 16:36:46 +00:00
|
|
|
|
id="fields.E339",
|
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-02-19 18:01:55 +00:00
|
|
|
|
|
2014-03-07 11:56:28 +00:00
|
|
|
|
def test_invalid_field(self):
|
2014-02-19 18:01:55 +00:00
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
|
Providing invalid field names to ManyToManyField.through_fields
|
2014-03-07 11:56:28 +00:00
|
|
|
|
triggers validation errors.
|
2014-02-19 18:01:55 +00:00
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-02-19 18:01:55 +00:00
|
|
|
|
class Fan(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Event(models.Model):
|
2015-09-11 23:33:12 +00:00
|
|
|
|
invitees = models.ManyToManyField(
|
|
|
|
|
Fan,
|
|
|
|
|
through="Invitation",
|
|
|
|
|
through_fields=("invalid_field_1", "invalid_field_2"),
|
|
|
|
|
)
|
2014-02-19 18:01:55 +00:00
|
|
|
|
|
|
|
|
|
class Invitation(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
event = models.ForeignKey(Event, models.CASCADE)
|
|
|
|
|
invitee = models.ForeignKey(Fan, models.CASCADE)
|
|
|
|
|
inviter = models.ForeignKey(Fan, models.CASCADE, related_name="+")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-03-07 11:56:28 +00:00
|
|
|
|
field = Event._meta.get_field("invitees")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Event),
|
|
|
|
|
[
|
2014-03-07 11:56:28 +00:00
|
|
|
|
Error(
|
2015-09-11 23:33:12 +00:00
|
|
|
|
"The intermediary model 'invalid_models_tests.Invitation' has no "
|
|
|
|
|
"field 'invalid_field_1'.",
|
2014-03-07 11:56:28 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Did you mean one of the following foreign keys to 'Event': "
|
|
|
|
|
"event?"
|
|
|
|
|
),
|
|
|
|
|
obj=field,
|
2016-02-12 16:36:46 +00:00
|
|
|
|
id="fields.E338",
|
|
|
|
|
),
|
2014-03-07 11:56:28 +00:00
|
|
|
|
Error(
|
2015-09-11 23:33:12 +00:00
|
|
|
|
"The intermediary model 'invalid_models_tests.Invitation' has no "
|
|
|
|
|
"field 'invalid_field_2'.",
|
2014-03-07 11:56:28 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Did you mean one of the following foreign keys to 'Fan': "
|
|
|
|
|
"invitee, inviter?"
|
|
|
|
|
),
|
|
|
|
|
obj=field,
|
2016-02-12 16:36:46 +00:00
|
|
|
|
id="fields.E338",
|
|
|
|
|
),
|
2017-07-11 08:53:22 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
2014-02-19 18:01:55 +00:00
|
|
|
|
|
|
|
|
|
def test_explicit_field_names(self):
|
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
|
If ``through_fields`` kwarg is given, it must specify both
|
2014-02-19 18:01:55 +00:00
|
|
|
|
link fields of the intermediary table.
|
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-02-19 18:01:55 +00:00
|
|
|
|
class Fan(models.Model):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Event(models.Model):
|
|
|
|
|
invitees = models.ManyToManyField(
|
|
|
|
|
Fan, through="Invitation", through_fields=(None, "invitee")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
)
|
2014-02-19 18:01:55 +00:00
|
|
|
|
|
|
|
|
|
class Invitation(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
|
event = models.ForeignKey(Event, models.CASCADE)
|
|
|
|
|
invitee = models.ForeignKey(Fan, models.CASCADE)
|
|
|
|
|
inviter = models.ForeignKey(Fan, models.CASCADE, related_name="+")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2014-02-19 18:01:55 +00:00
|
|
|
|
field = Event._meta.get_field("invitees")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Event),
|
|
|
|
|
[
|
2014-02-19 18:01:55 +00:00
|
|
|
|
Error(
|
2015-09-11 23:33:12 +00:00
|
|
|
|
"Field specifies 'through_fields' but does not provide the names "
|
|
|
|
|
"of the two link fields that should be used for the relation "
|
|
|
|
|
"through model 'invalid_models_tests.Invitation'.",
|
2016-02-12 16:36:46 +00:00
|
|
|
|
hint=(
|
|
|
|
|
"Make sure you specify 'through_fields' as "
|
|
|
|
|
"through_fields=('field1', 'field2')"
|
|
|
|
|
),
|
2014-02-19 18:01:55 +00:00
|
|
|
|
obj=field,
|
2017-07-11 08:53:22 +00:00
|
|
|
|
id="fields.E337",
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
2015-10-09 16:55:19 +00:00
|
|
|
|
|
|
|
|
|
def test_superset_foreign_object(self):
|
|
|
|
|
class Parent(models.Model):
|
|
|
|
|
a = models.PositiveIntegerField()
|
|
|
|
|
b = models.PositiveIntegerField()
|
|
|
|
|
c = models.PositiveIntegerField()
|
|
|
|
|
|
|
|
|
|
class Meta:
|
2015-10-13 13:08:27 +00:00
|
|
|
|
unique_together = (("a", "b", "c"),)
|
2015-10-09 16:55:19 +00:00
|
|
|
|
|
|
|
|
|
class Child(models.Model):
|
|
|
|
|
a = models.PositiveIntegerField()
|
|
|
|
|
b = models.PositiveIntegerField()
|
|
|
|
|
value = models.CharField(max_length=255)
|
2019-08-20 07:54:41 +00:00
|
|
|
|
parent = models.ForeignObject(
|
2015-10-09 16:55:19 +00:00
|
|
|
|
Parent,
|
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
|
from_fields=("a", "b"),
|
|
|
|
|
to_fields=("a", "b"),
|
|
|
|
|
related_name="children",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
field = Child._meta.get_field("parent")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Child),
|
|
|
|
|
[
|
2015-10-09 16:55:19 +00:00
|
|
|
|
Error(
|
2015-10-13 13:08:27 +00:00
|
|
|
|
"No subset of the fields 'a', 'b' on model 'Parent' is unique.",
|
|
|
|
|
hint=(
|
2020-02-19 21:57:16 +00:00
|
|
|
|
"Mark a single field as unique=True or add a set of "
|
|
|
|
|
"fields to a unique constraint (via unique_together or a "
|
|
|
|
|
"UniqueConstraint (without condition) in the model "
|
|
|
|
|
"Meta.constraints)."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2015-10-09 16:55:19 +00:00
|
|
|
|
obj=field,
|
|
|
|
|
id="fields.E310",
|
2015-10-13 13:08:27 +00:00
|
|
|
|
),
|
2015-10-09 16:55:19 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|
2015-10-09 16:55:19 +00:00
|
|
|
|
|
2015-12-02 23:55:50 +00:00
|
|
|
|
def test_intersection_foreign_object(self):
|
2015-10-09 16:55:19 +00:00
|
|
|
|
class Parent(models.Model):
|
|
|
|
|
a = models.PositiveIntegerField()
|
|
|
|
|
b = models.PositiveIntegerField()
|
|
|
|
|
c = models.PositiveIntegerField()
|
|
|
|
|
d = models.PositiveIntegerField()
|
|
|
|
|
|
|
|
|
|
class Meta:
|
2015-10-13 13:08:27 +00:00
|
|
|
|
unique_together = (("a", "b", "c"),)
|
2015-10-09 16:55:19 +00:00
|
|
|
|
|
|
|
|
|
class Child(models.Model):
|
|
|
|
|
a = models.PositiveIntegerField()
|
|
|
|
|
b = models.PositiveIntegerField()
|
|
|
|
|
d = models.PositiveIntegerField()
|
|
|
|
|
value = models.CharField(max_length=255)
|
2019-08-20 07:54:41 +00:00
|
|
|
|
parent = models.ForeignObject(
|
2015-10-09 16:55:19 +00:00
|
|
|
|
Parent,
|
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
|
from_fields=("a", "b", "d"),
|
|
|
|
|
to_fields=("a", "b", "d"),
|
|
|
|
|
related_name="children",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
field = Child._meta.get_field("parent")
|
2017-07-11 08:53:22 +00:00
|
|
|
|
self.assertEqual(
|
|
|
|
|
field.check(from_model=Child),
|
|
|
|
|
[
|
2015-10-09 16:55:19 +00:00
|
|
|
|
Error(
|
2015-10-13 13:08:27 +00:00
|
|
|
|
"No subset of the fields 'a', 'b', 'd' on model 'Parent' is "
|
|
|
|
|
"unique.",
|
|
|
|
|
hint=(
|
2020-02-19 21:57:16 +00:00
|
|
|
|
"Mark a single field as unique=True or add a set of "
|
|
|
|
|
"fields to a unique constraint (via unique_together or a "
|
|
|
|
|
"UniqueConstraint (without condition) in the model "
|
|
|
|
|
"Meta.constraints)."
|
2022-02-03 19:24:19 +00:00
|
|
|
|
),
|
2015-10-09 16:55:19 +00:00
|
|
|
|
obj=field,
|
|
|
|
|
id="fields.E310",
|
2015-10-13 13:08:27 +00:00
|
|
|
|
),
|
2015-10-09 16:55:19 +00:00
|
|
|
|
],
|
2017-07-11 08:53:22 +00:00
|
|
|
|
)
|