Improved model validator to validate 'choices' for a field

git-svn-id: http://code.djangoproject.com/svn/django/trunk@509 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-08-15 19:00:50 +00:00
parent 56071754ea
commit 7cfddb3c43
1 changed files with 7 additions and 0 deletions

View File

@ -506,6 +506,13 @@ def validate():
for f in opts.fields: for f in opts.fields:
if isinstance(f, meta.CharField) and f.maxlength in (None, 0): if isinstance(f, meta.CharField) and f.maxlength in (None, 0):
e.add(opts, '"%s" field: CharFields require a "maxlength" attribute.' % f.name) e.add(opts, '"%s" field: CharFields require a "maxlength" attribute.' % f.name)
if f.choices:
if not type(f.choices) in (tuple, list):
e.add(opts, '"%s" field: "choices" should be either a tuple or list.' % f.name)
else:
for c in f.choices:
if not type(c) in (tuple, list) or len(c) != 2:
e.add(opts, '"%s" field: "choices" should be a sequence of two-tuples.' % f.name)
# Check admin attribute. # Check admin attribute.
if opts.admin is not None and not isinstance(opts.admin, meta.Admin): if opts.admin is not None and not isinstance(opts.admin, meta.Admin):