Ticket #4312: models.diff

File models.diff, 1.8 KB (added by ctdecci@…, 17 years ago)
  • django/newforms/models.py

     
    1212__all__ = ('save_instance', 'form_for_model', 'form_for_instance', 'form_for_fields',
    1313           'ModelChoiceField', 'ModelMultipleChoiceField')
    1414
    15 def save_instance(form, instance, fields=None, fail_message='saved', commit=True):
     15def save_instance(form, instance, fields=None, defaults=None, fail_message='saved', commit=True):
    1616    """
    1717    Saves bound Form ``form``'s cleaned_data into model instance ``instance``.
    1818
     
    3131        if fields and f.name not in fields:
    3232            continue
    3333        setattr(instance, f.name, cleaned_data[f.name])
     34    if defaults:
     35        for f in defaults:
     36            setattr(instance, f, defaults[f])
    3437    if commit:
    3538        instance.save()
    3639        for f in opts.many_to_many:
     
    4649
    4750def make_model_save(model, fields, fail_message):
    4851    "Returns the save() method for a Form."
    49     def save(self, commit=True):
    50         return save_instance(self, model(), fields, fail_message, commit)
     52    def save(self, commit=True, defaults=None):
     53        return save_instance(self, model(), fields, defaults, fail_message, commit)
    5154    return save
    5255   
    5356def make_instance_save(instance, fields, fail_message):
    5457    "Returns the save() method for a Form."
    55     def save(self, commit=True):
    56         return save_instance(self, instance, fields, fail_message, commit)
     58    def save(self, commit=True, defaults=None):
     59        return save_instance(self, instance, fields, defaults, fail_message, commit)
    5760    return save
    5861
    5962def form_for_model(model, form=BaseForm, fields=None, formfield_callback=lambda f: f.formfield()):
Back to Top