Django

Code

Changeset 6940

Show
Ignore:
Timestamp:
12/17/07 05:59:53 (1 year ago)
Author:
mtredinnick
Message:

Fixed #6197 -- Added (optional) formfield_callback argument to ModelForms?.new. Patch from guido@python.org.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/models.py

    r6917 r6940  
    150150    Returns a dict containing the data in ``instance`` suitable for passing as 
    151151    a Form's ``initial`` keyword argument. 
    152      
     152 
    153153    ``fields`` is an optional list of field names. If provided, only the named 
    154154    fields will be included in the returned dict. 
    155      
     155 
    156156    ``exclude`` is an optional list of field names. If provided, the named 
    157157    fields will be excluded from the returned dict, even if they are listed in 
     
    188188    ``fields`` is an optional list of field names. If provided, only the named 
    189189    fields will be included in the returned fields. 
    190      
     190 
    191191    ``exclude`` is an optional list of field names. If provided, the named 
    192192    fields will be excluded from the returned fields, even if they are listed 
     
    215215 
    216216class ModelFormMetaclass(type): 
    217     def __new__(cls, name, bases, attrs): 
    218         # TODO: no way to specify formfield_callback yet, do we need one, or 
    219         # should it be a special case for the admin? 
     217    def __new__(cls, name, bases, attrs, 
     218                formfield_callback=lambda f: f.formfield()): 
    220219        fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)] 
    221220        fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) 
     
    254253                if base_model and base_model is not opts.model: 
    255254                    raise ImproperlyConfigured('%s defines a different model than its parent.' % name) 
    256             model_fields = fields_for_model(opts.model, opts.fields, opts.exclude) 
     255            model_fields = fields_for_model(opts.model, opts.fields, 
     256                    opts.exclude, formfield_callback) 
    257257            # fields declared in base classes override fields from the model 
    258258            model_fields.update(declared_fields)