newforms: Added initial implementation of form_for_model and form_for_fields

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4205 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-12-15 05:46:11 +00:00
parent 0a7d8b18ff
commit 6001974e45
4 changed files with 61 additions and 5 deletions

View File

@ -333,6 +333,12 @@ class Field(object):
return self._choices return self._choices
choices = property(_get_choices) choices = property(_get_choices)
def formfield(self):
"Returns a django.newforms.Field instance for this database Field."
from django.newforms import CharField
# TODO: This is just a temporary default during development.
return CharField(label=capfirst(self.verbose_name))
class AutoField(Field): class AutoField(Field):
empty_strings_allowed = False empty_strings_allowed = False
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):

View File

@ -1,13 +1,19 @@
""" """
Helper functions for creating Forms from Django models and database field objects. Helper functions for creating Form classes from Django models
and database field objects.
""" """
from forms import BaseForm, DeclarativeFieldsMetaclass, SortedDictFromList
__all__ = ('form_for_model', 'form_for_fields') __all__ = ('form_for_model', 'form_for_fields')
def form_for_model(model): def form_for_model(model):
"Returns a Form instance for the given Django model class." "Returns a Form class for the given Django model class."
raise NotImplementedError opts = model._meta
fields = SortedDictFromList([(f.name, f.formfield()) for f in opts.fields + opts.many_to_many])
return type(opts.object_name + 'Form', (BaseForm,), {'fields': fields, '_model_opts': opts})
def form_for_fields(field_list): def form_for_fields(field_list):
"Returns a Form instance for the given list of Django database field instances." "Returns a Form class for the given list of Django database field instances."
raise NotImplementedError fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list])
return type('FormForFields', (BaseForm,), {'fields': fields})

View File

View File

@ -0,0 +1,44 @@
"""
34. Generating HTML forms from models
Django provides shortcuts for creating Form objects from a model class.
"""
from django.db import models
class Category(models.Model):
name = models.CharField(maxlength=20)
url = models.CharField('The URL', maxlength=20)
def __str__(self):
return self.name
class Article(models.Model):
headline = models.CharField(maxlength=50)
pub_date = models.DateTimeField()
categories = models.ManyToManyField(Category)
def __str__(self):
return self.headline
__test__ = {'API_TESTS': """
>>> from django.newforms import form_for_model
>>> 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 type="text" name="name" id="id_name" /></td></tr>
<tr><th><label for="id_url">The URL:</label></th><td><input type="text" name="url" id="id_url" /></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 type="text" name="name" id="id_name" /></li>
<li><label for="id_url">The URL:</label> <input type="text" name="url" id="id_url" /></li>
>>> print f['name']
<input type="text" name="name" id="id_name" />
>>> f = CategoryForm(auto_id=False)
>>> print f.as_ul()
<li>ID: <input type="text" name="id" /></li>
<li>Name: <input type="text" name="name" /></li>
<li>The URL: <input type="text" name="url" /></li>
"""}