Fixed #27295 -- Added a system check to prohibit model names that start or end with an underscore or contain double underscores.

This commit is contained in:
Quentin Fulsher 2016-10-11 00:59:17 -07:00 committed by Tim Graham
parent 41759c9082
commit f62abfc03d
4 changed files with 73 additions and 2 deletions

View File

@ -1265,7 +1265,11 @@ class Model(six.with_metaclass(ModelBase)):
errors.extend(cls._check_fields(**kwargs)) errors.extend(cls._check_fields(**kwargs))
errors.extend(cls._check_m2m_through_same_relationship()) errors.extend(cls._check_m2m_through_same_relationship())
errors.extend(cls._check_long_column_names()) errors.extend(cls._check_long_column_names())
clash_errors = cls._check_id_field() + cls._check_field_name_clashes() clash_errors = (
cls._check_id_field() +
cls._check_field_name_clashes() +
cls._check_model_name_db_lookup_clashes()
)
errors.extend(clash_errors) errors.extend(clash_errors)
# If there are field name clashes, hide consequent column name # If there are field name clashes, hide consequent column name
# clashes. # clashes.
@ -1469,6 +1473,30 @@ class Model(six.with_metaclass(ModelBase)):
return errors return errors
@classmethod
def _check_model_name_db_lookup_clashes(cls):
errors = []
model_name = cls.__name__
if model_name.startswith('_') or model_name.endswith('_'):
errors.append(
checks.Error(
"The model name '%s' cannot start or end with an underscore "
"as it collides with the query lookup syntax." % model_name,
obj=cls,
id='models.E023'
)
)
elif LOOKUP_SEP in model_name:
errors.append(
checks.Error(
"The model name '%s' cannot contain double underscores as "
"it collides with the query lookup syntax." % model_name,
obj=cls,
id='models.E024'
)
)
return errors
@classmethod @classmethod
def _check_index_together(cls): def _check_index_together(cls):
""" Check the value of "index_together" option. """ """ Check the value of "index_together" option. """

View File

@ -141,6 +141,10 @@ Models
* **models.E022**: ``<function>`` contains a lazy reference to * **models.E022**: ``<function>`` contains a lazy reference to
``<app label>.<model>``, but app ``<app label>`` isn't installed or ``<app label>.<model>``, but app ``<app label>`` isn't installed or
doesn't provide model ``<model>``. doesn't provide model ``<model>``.
* **models.E023**: The model name ``<model>`` cannot start or end with an
underscore as it collides with the query lookup syntax.
* **models.E024**: The model name ``<model>`` cannot contain double underscores
as it collides with the query lookup syntax.
Fields Fields
~~~~~~ ~~~~~~

View File

@ -689,6 +689,45 @@ class OtherModelTests(SimpleTestCase):
self.assertFalse(Child.check()) self.assertFalse(Child.check())
def test_name_beginning_with_underscore(self):
class _Model(models.Model):
pass
self.assertEqual(_Model.check(), [
Error(
"The model name '_Model' cannot start or end with an underscore "
"as it collides with the query lookup syntax.",
obj=_Model,
id='models.E023',
)
])
def test_name_ending_with_underscore(self):
class Model_(models.Model):
pass
self.assertEqual(Model_.check(), [
Error(
"The model name 'Model_' cannot start or end with an underscore "
"as it collides with the query lookup syntax.",
obj=Model_,
id='models.E023',
)
])
def test_name_contains_double_underscores(self):
class Test__Model(models.Model):
pass
self.assertEqual(Test__Model.check(), [
Error(
"The model name 'Test__Model' cannot contain double underscores "
"as it collides with the query lookup syntax.",
obj=Test__Model,
id='models.E024',
)
])
@override_settings(TEST_SWAPPED_MODEL_BAD_VALUE='not-a-model') @override_settings(TEST_SWAPPED_MODEL_BAD_VALUE='not-a-model')
def test_swappable_missing_app_name(self): def test_swappable_missing_app_name(self):
class Model(models.Model): class Model(models.Model):

View File

@ -794,7 +794,7 @@ class RelativeFieldTests(SimpleTestCase):
pass pass
for related_name in related_names: for related_name in related_names:
Child = type(str('Child_%s') % str(related_name), (models.Model,), { Child = type(str('Child%s') % str(related_name), (models.Model,), {
'parent': models.ForeignKey('Parent', models.CASCADE, related_name=related_name), 'parent': models.ForeignKey('Parent', models.CASCADE, related_name=related_name),
'__module__': Parent.__module__, '__module__': Parent.__module__,
}) })