Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #6197 -- Added (optional) formfield_callback argument to ModelF…

…orms.__new__. Patch from guido@python.org.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6940 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ec72071615f31a81ad171e184ec5327f81a94286 1 parent b279b75
@malcolmt malcolmt authored
Showing with 7 additions and 7 deletions.
  1. +7 −7 django/newforms/models.py
View
14 django/newforms/models.py
@@ -149,10 +149,10 @@ def model_to_dict(instance, fields=None, exclude=None):
"""
Returns a dict containing the data in ``instance`` suitable for passing as
a Form's ``initial`` keyword argument.
-
+
``fields`` is an optional list of field names. If provided, only the named
fields will be included in the returned dict.
-
+
``exclude`` is an optional list of field names. If provided, the named
fields will be excluded from the returned dict, even if they are listed in
the ``fields`` argument.
@@ -187,7 +187,7 @@ def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda
``fields`` is an optional list of field names. If provided, only the named
fields will be included in the returned fields.
-
+
``exclude`` is an optional list of field names. If provided, the named
fields will be excluded from the returned fields, even if they are listed
in the ``fields`` argument.
@@ -214,9 +214,8 @@ def __init__(self, options=None):
self.exclude = getattr(options, 'exclude', None)
class ModelFormMetaclass(type):
- def __new__(cls, name, bases, attrs):
- # TODO: no way to specify formfield_callback yet, do we need one, or
- # should it be a special case for the admin?
+ def __new__(cls, name, bases, attrs,
+ formfield_callback=lambda f: f.formfield()):
fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)]
fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))
@@ -253,7 +252,8 @@ def __new__(cls, name, bases, attrs):
base_model = getattr(base_opts, 'model', None)
if base_model and base_model is not opts.model:
raise ImproperlyConfigured('%s defines a different model than its parent.' % name)
- model_fields = fields_for_model(opts.model, opts.fields, opts.exclude)
+ model_fields = fields_for_model(opts.model, opts.fields,
+ opts.exclude, formfield_callback)
# fields declared in base classes override fields from the model
model_fields.update(declared_fields)
attrs['base_fields'] = model_fields
Please sign in to comment.
Something went wrong with that request. Please try again.