Changeset 4387
- Timestamp:
- 01/22/07 00:10:47 (2 years ago)
- Files:
-
- django/trunk/django/newforms/models.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/newforms/models.py
r4300 r4387 48 48 return save 49 49 50 def form_for_model(model, form=BaseForm ):50 def form_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formfield()): 51 51 """ 52 52 Returns a Form class for the given Django model class. 53 53 54 54 Provide ``form`` if you want to use a custom BaseForm subclass. 55 56 Provide ``formfield_callback`` if you want to define different logic for 57 determining the formfield for a given database field. It's a callable that 58 takes a database Field instance and returns a form Field instance. 55 59 """ 56 60 opts = model._meta 57 61 field_list = [] 58 62 for f in opts.fields + opts.many_to_many: 59 formfield = f .formfield()63 formfield = formfield_callback(f) 60 64 if formfield: 61 65 field_list.append((f.name, formfield)) … … 63 67 return type(opts.object_name + 'Form', (form,), {'fields': fields, '_model': model, 'save': model_save}) 64 68 65 def form_for_instance(instance, form=BaseForm ):69 def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): 66 70 """ 67 71 Returns a Form class for the given Django model instance. 68 72 69 73 Provide ``form`` if you want to use a custom BaseForm subclass. 74 75 Provide ``formfield_callback`` if you want to define different logic for 76 determining the formfield for a given database field. It's a callable that 77 takes a database Field instance, plus **kwargs, and returns a form Field 78 instance with the given kwargs (i.e. 'initial'). 70 79 """ 71 80 model = instance.__class__ … … 74 83 for f in opts.fields + opts.many_to_many: 75 84 current_value = f.value_from_object(instance) 76 formfield = f .formfield(initial=current_value)85 formfield = formfield_callback(f, initial=current_value) 77 86 if formfield: 78 87 field_list.append((f.name, formfield))
