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
This commit is contained in:
Malcolm Tredinnick 2007-10-21 21:53:18 +00:00
parent ef454b8a14
commit 8e87587e89
2 changed files with 6 additions and 2 deletions

View File

@ -35,6 +35,8 @@ def get_validation_errors(outfile, app=None):
for f in opts.fields: for f in opts.fields:
if f.name == 'id' and not f.primary_key and opts.pk.name == 'id': 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) 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): if isinstance(f, models.CharField) and f.max_length in (None, 0):
e.add(opts, '"%s": CharFields require a "max_length" attribute.' % f.name) e.add(opts, '"%s": CharFields require a "max_length" attribute.' % f.name)
if isinstance(f, models.DecimalField): if isinstance(f, models.DecimalField):

View File

@ -14,6 +14,7 @@ class FieldErrors(models.Model):
choices = models.CharField(max_length=10, choices='bad') choices = models.CharField(max_length=10, choices='bad')
choices2 = models.CharField(max_length=10, choices=[(1,2,3),(1,2,3)]) choices2 = models.CharField(max_length=10, choices=[(1,2,3),(1,2,3)])
index = models.CharField(max_length=10, db_index='bad') index = models.CharField(max_length=10, db_index='bad')
field_ = models.CharField(max_length=10)
class Target(models.Model): class Target(models.Model):
tgt_safe = models.CharField(max_length=10) tgt_safe = models.CharField(max_length=10)
@ -47,7 +48,7 @@ class Target2(models.Model):
class Clash3(models.Model): class Clash3(models.Model):
src_safe = models.CharField(max_length=10, core=True) src_safe = models.CharField(max_length=10, core=True)
foreign_1 = models.ForeignKey(Target2, related_name='foreign_tgt') foreign_1 = models.ForeignKey(Target2, related_name='foreign_tgt')
foreign_2 = models.ForeignKey(Target2, related_name='m2m_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 # on self don't require a related accessor, so many potential
# clashes are avoided. # clashes are avoided.
validm2m_set = models.ManyToManyField("ValidM2M") validm2m_set = models.ManyToManyField("ValidM2M")
m2m_1 = models.ManyToManyField("ValidM2M", related_name='id') m2m_1 = models.ManyToManyField("ValidM2M", related_name='id')
m2m_2 = models.ManyToManyField("ValidM2M", related_name='src_safe') 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: "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: "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 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: 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'. invalid_models.clash1: Reverse query name for field 'foreign' clashes with field 'Target.clash1'. Add a related_name argument to the definition for 'foreign'.