Ticket #4001: save_instance_m2m.patch
File save_instance_m2m.patch, 1.8 KB (added by , 18 years ago) |
---|
-
django/newforms/models.py
35 35 if form.errors: 36 36 raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) 37 37 clean_data = form.clean_data 38 # If many-to-many data is given and the record doesn't exist in the 39 # database yet, the many-to-many data will be lost. This happens because 40 # many-to-many options cannot be set on an object until after it's saved. 41 if not commit: 42 many_to_many_data = False 43 for f in opts.many_to_many: 44 if clean_data.get(f.name): 45 many_to_many_data = True 46 break 47 if many_to_many_data: 48 try: 49 instance.__class__._default_manager.get(pk=instance._get_pk_val()) 50 except instance.DoesNotExist: 51 raise ValueError("The %s could not be created because many-to-many data was provided for this unsaved record." % opts.object_name) 38 52 for f in opts.fields: 39 53 if not f.editable or isinstance(f, models.AutoField) or not f.name in clean_data: 40 54 continue 41 55 setattr(instance, f.name, clean_data[f.name]) 42 56 if commit: 43 57 instance.save() 58 if instance._get_pk_val(): 44 59 for f in opts.many_to_many: 45 60 if f.name in clean_data: 46 61 setattr(instance, f.attname, clean_data[f.name]) 47 # GOTCHA: If many-to-many data is given and commit=False, the many-to-many48 # data will be lost. This happens because a many-to-many options cannot be49 # set on an object until after it's saved. Maybe we should raise an50 # exception in that case.51 62 return instance 52 63 53 64 def make_instance_save(instance):