0
0
mirror of https://github.com/django/django.git synced 2024-11-24 20:07:01 +01:00

Refs #35224 -- Removed special-case GenericForeignKey system check.

This commit is contained in:
Adam Johnson 2024-02-22 23:07:08 +00:00
parent 1fd57f2a21
commit 40cf958393
7 changed files with 32 additions and 31 deletions

View File

@ -1,8 +1,5 @@
from django.apps import AppConfig from django.apps import AppConfig
from django.contrib.contenttypes.checks import ( from django.contrib.contenttypes.checks import check_model_name_lengths
check_generic_foreign_keys,
check_model_name_lengths,
)
from django.core import checks from django.core import checks
from django.db.models.signals import post_migrate, pre_migrate from django.db.models.signals import post_migrate, pre_migrate
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -18,5 +15,4 @@ class ContentTypesConfig(AppConfig):
def ready(self): def ready(self):
pre_migrate.connect(inject_rename_contenttypes_operations, sender=self) pre_migrate.connect(inject_rename_contenttypes_operations, sender=self)
post_migrate.connect(create_contenttypes) post_migrate.connect(create_contenttypes)
checks.register(check_generic_foreign_keys, checks.Tags.models)
checks.register(check_model_name_lengths, checks.Tags.models) checks.register(check_model_name_lengths, checks.Tags.models)

View File

@ -4,27 +4,6 @@ from django.apps import apps
from django.core.checks import Error from django.core.checks import Error
def check_generic_foreign_keys(app_configs=None, **kwargs):
from .fields import GenericForeignKey
if app_configs is None:
models = apps.get_models()
else:
models = chain.from_iterable(
app_config.get_models() for app_config in app_configs
)
errors = []
fields = (
obj
for model in models
for obj in vars(model).values()
if isinstance(obj, GenericForeignKey)
)
for field in fields:
errors.extend(field.check())
return errors
def check_model_name_lengths(app_configs=None, **kwargs): def check_model_name_lengths(app_configs=None, **kwargs):
if app_configs is None: if app_configs is None:
models = apps.get_models() models = apps.get_models()

View File

@ -357,7 +357,10 @@ class GenericRelation(ForeignObject):
def check(self, **kwargs): def check(self, **kwargs):
return [ return [
*super().check(**kwargs), *self._check_field_name(),
*self._check_related_query_name_is_valid(),
*self._check_relation_model_exists(),
*self._check_referencing_to_swapped_model(),
*self._check_generic_foreign_key_existence(), *self._check_generic_foreign_key_existence(),
] ]

View File

@ -1789,6 +1789,8 @@ class Model(AltersData, metaclass=ModelBase):
errors = [] errors = []
for field in cls._meta.local_fields: for field in cls._meta.local_fields:
errors.extend(field.check(**kwargs)) errors.extend(field.check(**kwargs))
for field in cls._meta.private_fields:
errors.extend(field.check(**kwargs))
for field in cls._meta.local_many_to_many: for field in cls._meta.local_many_to_many:
errors.extend(field.check(from_model=cls, **kwargs)) errors.extend(field.check(from_model=cls, **kwargs))
return errors return errors

View File

@ -122,10 +122,10 @@ class GenericForeignKeyTests(SimpleTestCase):
with mock.patch.object(GenericForeignKey, "check") as check: with mock.patch.object(GenericForeignKey, "check") as check:
checks.run_checks(app_configs=self.apps.get_app_configs()) checks.run_checks(app_configs=self.apps.get_app_configs())
check.assert_called_once_with() check.assert_called_once_with(databases=None)
@isolate_apps("contenttypes_tests") @isolate_apps("contenttypes_tests", attr_name="apps")
class GenericRelationTests(SimpleTestCase): class GenericRelationTests(SimpleTestCase):
def test_valid_generic_relationship(self): def test_valid_generic_relationship(self):
class TaggedItem(models.Model): class TaggedItem(models.Model):
@ -253,6 +253,19 @@ class GenericRelationTests(SimpleTestCase):
], ],
) )
def test_generic_relation_checks_are_performed(self):
class TaggedItem(models.Model):
content_type = models.ForeignKey(ContentType, models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()
class InvalidBookmark(models.Model):
tags_ = GenericRelation("TaggedItem")
with mock.patch.object(GenericRelation, "check") as check:
checks.run_checks(app_configs=self.apps.get_app_configs())
check.assert_called_once_with(databases=None)
@isolate_apps("contenttypes_tests", attr_name="apps") @isolate_apps("contenttypes_tests", attr_name="apps")
class ModelCheckTests(SimpleTestCase): class ModelCheckTests(SimpleTestCase):

View File

@ -411,6 +411,12 @@ class RelatedJSONModel(models.Model):
required_db_features = {"supports_json_field"} required_db_features = {"supports_json_field"}
class GfkModel(models.Model):
object_id = models.PositiveIntegerField()
content_type = models.ForeignKey(ContentType, models.CASCADE)
gfk = GenericForeignKey()
class AllFieldsModel(models.Model): class AllFieldsModel(models.Model):
big_integer = models.BigIntegerField() big_integer = models.BigIntegerField()
binary = models.BinaryField() binary = models.BinaryField()
@ -448,7 +454,7 @@ class AllFieldsModel(models.Model):
object_id = models.PositiveIntegerField() object_id = models.PositiveIntegerField()
content_type = models.ForeignKey(ContentType, models.CASCADE) content_type = models.ForeignKey(ContentType, models.CASCADE)
gfk = GenericForeignKey() gfk = GenericForeignKey()
gr = GenericRelation(DataModel) gr = GenericRelation(GfkModel)
class ManyToMany(models.Model): class ManyToMany(models.Model):

View File

@ -5,7 +5,9 @@ from django.utils.translation import gettext_lazy as _
class Relation(models.Model): class Relation(models.Model):
pass content_type = models.ForeignKey(ContentType, models.CASCADE, related_name="+")
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()
class InstanceOnlyDescriptor: class InstanceOnlyDescriptor: