diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index a1537f0a94..fe317ac24f 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -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 diff --git a/django/newforms/models.py b/django/newforms/models.py index 6c01969001..58ed29e20f 100644 --- a/django/newforms/models.py +++ b/django/newforms/models.py @@ -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): diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 2f6cf340d4..c8dfa5a2a4 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -26,11 +26,9 @@ __test__ = {'API_TESTS': """ >>> CategoryForm = form_for_model(Category) >>> f = CategoryForm() >>> print f - >>> print f.as_ul() -
  • >>> print f['name'] @@ -38,7 +36,12 @@ __test__ = {'API_TESTS': """ >>> f = CategoryForm(auto_id=False) >>> print f.as_ul() -
  • ID:
  • Name:
  • The URL:
  • + +>>> f = CategoryForm({'name': 'Entertainment', 'url': 'entertainment'}) +>>> f.errors +{} +>>> f.clean_data +{'url': u'entertainment', 'name': u'Entertainment'} """}