diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 8e8d68aad5..1927ad7535 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -333,6 +333,12 @@ class Field(object): return self._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): empty_strings_allowed = False def __init__(self, *args, **kwargs): diff --git a/django/newforms/models.py b/django/newforms/models.py index d99619c44a..6c01969001 100644 --- a/django/newforms/models.py +++ b/django/newforms/models.py @@ -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') def form_for_model(model): - "Returns a Form instance for the given Django model class." - raise NotImplementedError + "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]) + return type(opts.object_name + 'Form', (BaseForm,), {'fields': fields, '_model_opts': opts}) def form_for_fields(field_list): - "Returns a Form instance for the given list of Django database field instances." - raise NotImplementedError + "Returns a Form class for the given list of Django database field instances." + fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list]) + return type('FormForFields', (BaseForm,), {'fields': fields}) diff --git a/tests/modeltests/model_forms/__init__.py b/tests/modeltests/model_forms/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py new file mode 100644 index 0000000000..b51b4e1a8b --- /dev/null +++ b/tests/modeltests/model_forms/models.py @@ -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 + + + +>>> print f.as_ul() +
  • +
  • +
  • +>>> print f['name'] + + +>>> f = CategoryForm(auto_id=False) +>>> print f.as_ul() +
  • ID:
  • +
  • Name:
  • +
  • The URL:
  • +"""}