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
This commit is contained in:
Malcolm Tredinnick 2007-12-17 11:59:53 +00:00
parent b279b75c87
commit ec72071615
1 changed files with 7 additions and 7 deletions

View File

@ -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 Returns a dict containing the data in ``instance`` suitable for passing as
a Form's ``initial`` keyword argument. a Form's ``initial`` keyword argument.
``fields`` is an optional list of field names. If provided, only the named ``fields`` is an optional list of field names. If provided, only the named
fields will be included in the returned dict. fields will be included in the returned dict.
``exclude`` is an optional list of field names. If provided, the named ``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 fields will be excluded from the returned dict, even if they are listed in
the ``fields`` argument. 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`` is an optional list of field names. If provided, only the named
fields will be included in the returned fields. fields will be included in the returned fields.
``exclude`` is an optional list of field names. If provided, the named ``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 fields will be excluded from the returned fields, even if they are listed
in the ``fields`` argument. in the ``fields`` argument.
@ -214,9 +214,8 @@ class ModelFormOptions(object):
self.exclude = getattr(options, 'exclude', None) self.exclude = getattr(options, 'exclude', None)
class ModelFormMetaclass(type): class ModelFormMetaclass(type):
def __new__(cls, name, bases, attrs): def __new__(cls, name, bases, attrs,
# TODO: no way to specify formfield_callback yet, do we need one, or formfield_callback=lambda f: f.formfield()):
# should it be a special case for the admin?
fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)] 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)) 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) base_model = getattr(base_opts, 'model', None)
if base_model and base_model is not opts.model: if base_model and base_model is not opts.model:
raise ImproperlyConfigured('%s defines a different model than its parent.' % name) 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 # fields declared in base classes override fields from the model
model_fields.update(declared_fields) model_fields.update(declared_fields)
attrs['base_fields'] = model_fields attrs['base_fields'] = model_fields