Author: mtredinnick
Date: 2007-10-21 16:53:18 -0500 (Sun, 21 Oct 2007)
New Revision: 6590

Modified:
   django/trunk/django/core/management/validation.py
   django/trunk/tests/modeltests/invalid_models/models.py
Log:
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).


Modified: django/trunk/django/core/management/validation.py
===================================================================
--- django/trunk/django/core/management/validation.py   2007-10-21 19:19:32 UTC 
(rev 6589)
+++ django/trunk/django/core/management/validation.py   2007-10-21 21:53:18 UTC 
(rev 6590)
@@ -35,6 +35,8 @@
         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):

Modified: django/trunk/tests/modeltests/invalid_models/models.py
===================================================================
--- django/trunk/tests/modeltests/invalid_models/models.py      2007-10-21 
19:19:32 UTC (rev 6589)
+++ django/trunk/tests/modeltests/invalid_models/models.py      2007-10-21 
21:53:18 UTC (rev 6590)
@@ -14,6 +14,7 @@
     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 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 @@
     # 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: "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'.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to