Ticket #3727: newforms_models_for_instance_exclude.diff
File newforms_models_for_instance_exclude.diff, 4.2 KB (added by , 18 years ago) |
---|
-
django/newforms/models.py
12 12 __all__ = ('save_instance', 'form_for_model', 'form_for_instance', 'form_for_fields', 13 13 'ModelChoiceField', 'ModelMultipleChoiceField') 14 14 15 def model_save(self, commit=True): 16 """ 17 Creates and returns model instance according to self.clean_data. 15 def make_model_save(exclude=[]): 16 def save(self, commit=True): 17 """ 18 Creates and returns model instance according to self.clean_data. 19 20 This method is created for any form_for_model Form. 21 """ 22 if self.errors: 23 raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name) 24 return save_instance(self, self._model(), commit, exclude=exclude) 25 return save 18 26 19 This method is created for any form_for_model Form. 27 def save_instance(form, instance, commit=True, exclude=[]): 20 28 """ 21 if self.errors:22 raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name)23 return save_instance(self, self._model(), commit)24 25 def save_instance(form, instance, commit=True):26 """27 29 Saves bound Form ``form``'s clean_data into model instance ``instance``. 28 30 29 31 Assumes ``form`` has a field for every non-AutoField database field in … … 36 38 raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) 37 39 clean_data = form.clean_data 38 40 for f in opts.fields: 39 if not f.editable or isinstance(f, models.AutoField) :41 if not f.editable or isinstance(f, models.AutoField) or f.name in exclude: 40 42 continue 41 43 setattr(instance, f.name, clean_data[f.name]) 42 44 if commit: … … 49 51 # exception in that case. 50 52 return instance 51 53 52 def make_instance_save(instance ):54 def make_instance_save(instance, exclude=[]): 53 55 "Returns the save() method for a form_for_instance Form." 54 56 def save(self, commit=True): 55 return save_instance(self, instance, commit )57 return save_instance(self, instance, commit, exclude=exclude) 56 58 return save 57 59 58 def form_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formfield() ):60 def form_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formfield(), exclude=[]): 59 61 """ 60 62 Returns a Form class for the given Django model class. 61 63 … … 68 70 opts = model._meta 69 71 field_list = [] 70 72 for f in opts.fields + opts.many_to_many: 71 if not f.editable :73 if not f.editable or f.name in exclude: 72 74 continue 73 75 formfield = formfield_callback(f) 74 76 if formfield: 75 77 field_list.append((f.name, formfield)) 76 78 fields = SortedDictFromList(field_list) 77 return type(opts.object_name + 'Form', (form,), {'base_fields': fields, '_model': model, 'save': m odel_save})79 return type(opts.object_name + 'Form', (form,), {'base_fields': fields, '_model': model, 'save': make_model_save(exclude)}) 78 80 79 def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs) ):81 def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs), exclude=[]): 80 82 """ 81 83 Returns a Form class for the given Django model instance. 82 84 … … 91 93 opts = model._meta 92 94 field_list = [] 93 95 for f in opts.fields + opts.many_to_many: 94 if not f.editable :96 if not f.editable or f.name in exclude: 95 97 continue 96 98 current_value = f.value_from_object(instance) 97 99 formfield = formfield_callback(f, initial=current_value) … … 99 101 field_list.append((f.name, formfield)) 100 102 fields = SortedDictFromList(field_list) 101 103 return type(opts.object_name + 'InstanceForm', (form,), 102 {'base_fields': fields, '_model': model, 'save': make_instance_save(instance )})104 {'base_fields': fields, '_model': model, 'save': make_instance_save(instance, exclude)}) 103 105 104 106 def form_for_fields(field_list): 105 107 "Returns a Form class for the given list of Django database field instances."