From 6a5deb6bc0ca6260b2af374583737c3c63a3037b Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 31 Mar 2007 09:47:32 +0000 Subject: [PATCH] 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 --- django/newforms/models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/django/newforms/models.py b/django/newforms/models.py index 616c7141e74..a60002b7058 100644 --- a/django/newforms/models.py +++ b/django/newforms/models.py @@ -36,13 +36,14 @@ def save_instance(form, instance, commit=True): raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) clean_data = form.clean_data 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 setattr(instance, f.name, clean_data[f.name]) if commit: instance.save() for f in opts.many_to_many: - setattr(instance, f.attname, clean_data[f.name]) + if f.name in clean_data: + setattr(instance, f.attname, clean_data[f.name]) # 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 # set on an object until after it's saved. Maybe we should raise an