Ticket #3232: apply_changes.patch
File apply_changes.patch, 4.5 KB (added by , 18 years ago) |
---|
-
django/newforms/forms.py
171 171 """ 172 172 return self.clean_data 173 173 174 def apply_changes(self, instance=None, save=True): 175 if not instance: 176 instance = getattr(self, 'default_instance', None) 177 if not instance: 178 raise ValueError('An instance is required') 179 opts = instance._meta 180 if self.errors: 181 raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) 182 # Generate a list of db fields for this instance. 183 db_fields = {} 184 for db_field in opts.fields + opts.many_to_many: 185 db_fields[db_field.attname] = db_field 186 # Save each field in the form which matches a db field. 187 for field in self.fields.keys(): 188 db_field = db_fields.get(field) 189 setattr(instance, db_field.attname, self.clean_data[field]) 190 if save: 191 instance.save() 192 return instance 193 174 194 class Form(BaseForm): 175 195 "A collection of Fields, plus their associated data." 176 196 # This is a separate class from BaseForm in order to abstract the way -
django/newforms/models.py
20 20 obj.save() 21 21 return obj 22 22 23 def make_apply_changes(opts, instance):24 "Returns the apply_changes() method for a form_for_instance Form."25 from django.db import models26 def apply_changes(self, save=True):27 if self.errors:28 raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name)29 clean_data = self.clean_data30 for f in opts.fields + opts.many_to_many:31 if isinstance(f, models.AutoField):32 continue33 setattr(instance, f.attname, clean_data[f.name])34 if save:35 instance.save()36 return instance37 return apply_changes38 39 23 def form_for_model(model, form=BaseForm): 40 24 """ 41 25 Returns a Form class for the given Django model class. … … 67 51 field_list.append((f.name, formfield)) 68 52 fields = SortedDictFromList(field_list) 69 53 return type(opts.object_name + 'InstanceForm', (form,), 70 {'fields': fields, '_model': model, ' apply_changes': make_apply_changes(opts, instance)})54 {'fields': fields, '_model': model, 'default_instance': instance}) 71 55 72 56 def form_for_fields(field_list): 73 57 "Returns a Form class for the given list of Django database field instances." -
tests/modeltests/model_forms/models.py
12 12 create(save=False), then you'll get the object without saving it. 13 13 14 14 The function django.newforms.form_for_instance() takes a model instance and 15 returns a Form that is tied to the instance. This form works just like any 16 other Form, with one additional method: apply_changes(). The apply_changes() 17 method updates the model instance. It saves the changes to the database if 18 apply_changes(save=True), which is default. If you pass save=False, then you'll 19 get the object without saving it. 15 returns a Form that is tied to the instance. 16 17 All forms now have the method apply_changes(instance), which can be used to 18 update a model instance. Using form_for_instance() you do not need to pass 19 an instance. It saves the changes to the database if apply_changes(save=True), 20 which is default. If you pass save=False, then the unsaved instance object 21 will be returned. 20 22 """ 21 23 22 24 from django.db import models … … 153 155 >>> f.say_hello() 154 156 hello 155 157 156 Use form_for_instance to create a Form from a model instance. There are two 157 differences between this Form and one created via form_for_model. First, the 158 object's current values are inserted as 'initial' data in each Field. Second, 159 the Form gets an apply_changes() method instead of a create() method. 158 Use form_for_instance to create a Form from a model instance. The difference 159 between this Form and a normal form is that the object's current values are 160 inserted as 'initial' data in each Field. 160 161 >>> w = Writer.objects.get(name='Mike Royko') 161 162 >>> RoykoForm = form_for_instance(w) 162 163 >>> f = RoykoForm(auto_id=False)