newforms: Changed form_for_model() to ignore a field if its formfield() returns None, and changed AutoField.formfield() to return None

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4214 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-12-15 21:22:13 +00:00
parent 5efb9272e2
commit 5cb093b033
3 changed files with 15 additions and 4 deletions

View File

@ -375,6 +375,9 @@ class AutoField(Field):
super(AutoField, self).contribute_to_class(cls, name)
cls._meta.has_auto_field = True
def formfield(self):
return None
class BooleanField(Field):
def __init__(self, *args, **kwargs):
kwargs['blank'] = True

View File

@ -10,7 +10,12 @@ __all__ = ('form_for_model', 'form_for_fields')
def form_for_model(model):
"Returns a Form class for the given Django model class."
opts = model._meta
fields = SortedDictFromList([(f.name, f.formfield()) for f in opts.fields + opts.many_to_many])
field_list = []
for f in opts.fields + opts.many_to_many:
formfield = f.formfield()
if formfield:
field_list.append((f.name, formfield))
fields = SortedDictFromList(field_list)
return type(opts.object_name + 'Form', (BaseForm,), {'fields': fields, '_model_opts': opts})
def form_for_fields(field_list):

View File

@ -26,11 +26,9 @@ __test__ = {'API_TESTS': """
>>> CategoryForm = form_for_model(Category)
>>> f = CategoryForm()
>>> print f
<tr><th><label for="id_id">ID:</label></th><td><input type="text" name="id" id="id_id" /></td></tr>
<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr>
<tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr>
>>> print f.as_ul()
<li><label for="id_id">ID:</label> <input type="text" name="id" id="id_id" /></li>
<li><label for="id_name">Name:</label> <input id="id_name" type="text" name="name" maxlength="20" /></li>
<li><label for="id_url">The URL:</label> <input id="id_url" type="text" name="url" maxlength="40" /></li>
>>> print f['name']
@ -38,7 +36,12 @@ __test__ = {'API_TESTS': """
>>> f = CategoryForm(auto_id=False)
>>> print f.as_ul()
<li>ID: <input type="text" name="id" /></li>
<li>Name: <input type="text" name="name" maxlength="20" /></li>
<li>The URL: <input type="text" name="url" maxlength="40" /></li>
>>> f = CategoryForm({'name': 'Entertainment', 'url': 'entertainment'})
>>> f.errors
{}
>>> f.clean_data
{'url': u'entertainment', 'name': u'Entertainment'}
"""}