From ec72071615f31a81ad171e184ec5327f81a94286 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Mon, 17 Dec 2007 11:59:53 +0000 Subject: [PATCH] Fixed #6197 -- Added (optional) formfield_callback argument to ModelForms.__new__. Patch from guido@python.org. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6940 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/models.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/django/newforms/models.py b/django/newforms/models.py index 17fd1cf2e2..44ff9f7d0a 100644 --- a/django/newforms/models.py +++ b/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 @@ class ModelFormOptions(object): 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 @@ class ModelFormMetaclass(type): 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