Fixed #3635 -- Fixed KeyError problem in newforms.model.save_instance().

Thanks, ludo@qix.it.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@4878 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-03-31 09:47:32 +00:00
parent 1ccdcbf765
commit 6a5deb6bc0
1 changed files with 3 additions and 2 deletions

View File

@ -36,12 +36,13 @@ def save_instance(form, instance, commit=True):
raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name)
clean_data = form.clean_data clean_data = form.clean_data
for f in opts.fields: for f in opts.fields:
if not f.editable or isinstance(f, models.AutoField): if not f.editable or isinstance(f, models.AutoField) or not f.name in clean_data:
continue continue
setattr(instance, f.name, clean_data[f.name]) setattr(instance, f.name, clean_data[f.name])
if commit: if commit:
instance.save() instance.save()
for f in opts.many_to_many: for f in opts.many_to_many:
if f.name in clean_data:
setattr(instance, f.attname, clean_data[f.name]) setattr(instance, f.attname, clean_data[f.name])
# GOTCHA: If many-to-many data is given and commit=False, the many-to-many # GOTCHA: If many-to-many data is given and commit=False, the many-to-many
# data will be lost. This happens because a many-to-many options cannot be # data will be lost. This happens because a many-to-many options cannot be