From 8e87587e89db8014300f0927799ea025fee53d15 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sun, 21 Oct 2007 21:53:18 +0000 Subject: [PATCH] Fixed #3265 -- Made it a validation error to have field names with trailing underscores. Allowing these would enable peopleto write ambiguous queryset filters (plus makes parsing filters much harder). git-svn-id: http://code.djangoproject.com/svn/django/trunk@6590 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management/validation.py | 2 ++ tests/modeltests/invalid_models/models.py | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/django/core/management/validation.py b/django/core/management/validation.py index 9e004d3d25..c433b7fa96 100644 --- a/django/core/management/validation.py +++ b/django/core/management/validation.py @@ -35,6 +35,8 @@ def get_validation_errors(outfile, app=None): for f in opts.fields: if f.name == 'id' and not f.primary_key and opts.pk.name == 'id': e.add(opts, '"%s": You can\'t use "id" as a field name, because each model automatically gets an "id" field if none of the fields have primary_key=True. You need to either remove/rename your "id" field or add primary_key=True to a field.' % f.name) + if f.name.endswith('_'): + e.add(opts, '"%s": Field names cannot end with underscores, because this would lead to ambiguous queryset filters.' % f.name) if isinstance(f, models.CharField) and f.max_length in (None, 0): e.add(opts, '"%s": CharFields require a "max_length" attribute.' % f.name) if isinstance(f, models.DecimalField): diff --git a/tests/modeltests/invalid_models/models.py b/tests/modeltests/invalid_models/models.py index 1afe660dd0..b746af6dba 100644 --- a/tests/modeltests/invalid_models/models.py +++ b/tests/modeltests/invalid_models/models.py @@ -14,6 +14,7 @@ class FieldErrors(models.Model): choices = models.CharField(max_length=10, choices='bad') choices2 = models.CharField(max_length=10, choices=[(1,2,3),(1,2,3)]) index = models.CharField(max_length=10, db_index='bad') + field_ = models.CharField(max_length=10) class Target(models.Model): tgt_safe = models.CharField(max_length=10) @@ -47,7 +48,7 @@ class Target2(models.Model): class Clash3(models.Model): src_safe = models.CharField(max_length=10, core=True) - + foreign_1 = models.ForeignKey(Target2, related_name='foreign_tgt') foreign_2 = models.ForeignKey(Target2, related_name='m2m_tgt') @@ -76,7 +77,7 @@ class ValidM2M(models.Model): # on self don't require a related accessor, so many potential # clashes are avoided. validm2m_set = models.ManyToManyField("ValidM2M") - + m2m_1 = models.ManyToManyField("ValidM2M", related_name='id') m2m_2 = models.ManyToManyField("ValidM2M", related_name='src_safe') @@ -116,6 +117,7 @@ invalid_models.fielderrors: "choices": "choices" should be iterable (e.g., a tup invalid_models.fielderrors: "choices2": "choices" should be a sequence of two-tuples. invalid_models.fielderrors: "choices2": "choices" should be a sequence of two-tuples. invalid_models.fielderrors: "index": "db_index" should be either None, True or False. +invalid_models.fielderrors: "field_": Field names cannot end with underscores, because this would lead to ambiguous queryset filters. invalid_models.clash1: Accessor for field 'foreign' clashes with field 'Target.clash1_set'. Add a related_name argument to the definition for 'foreign'. invalid_models.clash1: Accessor for field 'foreign' clashes with related m2m field 'Target.clash1_set'. Add a related_name argument to the definition for 'foreign'. invalid_models.clash1: Reverse query name for field 'foreign' clashes with field 'Target.clash1'. Add a related_name argument to the definition for 'foreign'.