Ticket #5126: ticket5126.diff

File ticket5126.diff, 1.5 KB (added by Christopher Lenz <cmlenz@…>, 17 years ago)

Patch as described in ticket description

  • django/newforms/models.py

     
    5454        form.save_m2m = save_m2m
    5555    return instance
    5656
     57def make_model_init(form, fields):
     58    """Returns the __init__() method for a Form."""
     59    def __init__(self, instance=None, **kwargs):
     60        if instance:
     61            opts = instance.__class__._meta
     62            initial = kwargs.setdefault('initial', {})
     63            for f in opts.fields:
     64                if not f.editable or f.name in initial:
     65                    continue
     66                if fields and f.name not in fields:
     67                    continue
     68                initial[f.name] = f.value_from_object(instance)
     69        form.__init__(self, **kwargs)
     70    return __init__
     71
    5772def make_model_save(model, fields, fail_message):
    5873    "Returns the save() method for a Form."
    5974    def save(self, commit=True):
     
    88103            field_list.append((f.name, formfield))
    89104    base_fields = SortedDictFromList(field_list)
    90105    return type(opts.object_name + 'Form', (form,),
    91         {'base_fields': base_fields, '_model': model, 'save': make_model_save(model, fields, 'created')})
     106        {'base_fields': base_fields, '_model': model, '__init__': make_model_init(form, fields),
     107         'save': make_model_save(model, fields, 'created')})
    92108
    93109def form_for_instance(instance, form=BaseForm, fields=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
    94110    """
Back to Top