diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2007-01-28 04:56:54 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2007-01-28 04:56:54 +0000 |
| commit | 83768bf067e84187780fbc749295d23bc048939b (patch) | |
| tree | 6eef04da0130648656b1bf9515071937c140e751 /django | |
| parent | 546f16d3232f4efc93c7eef0d88079f67d1d7d3d (diff) | |
Fixed #3263 -- newforms form_for_model() and form_for_instance() now handle foreign-key and many-to-many data properly. Thanks for the patch, Jeff Hilyard
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4439 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/newforms/models.py | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/django/newforms/models.py b/django/newforms/models.py index 326d8f620f..9bd6a3ff83 100644 --- a/django/newforms/models.py +++ b/django/newforms/models.py @@ -15,10 +15,7 @@ def model_save(self, commit=True): """ if self.errors: raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name) - obj = self._model(**self.clean_data) - if commit: - obj.save() - return obj + return save_instance(self, self._model(), commit) def save_instance(form, instance, commit=True): """ @@ -33,12 +30,18 @@ def save_instance(form, instance, commit=True): if form.errors: 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 + opts.many_to_many: + for f in opts.fields: if isinstance(f, models.AutoField): continue setattr(instance, f.attname, clean_data[f.name]) if commit: instance.save() + for f in opts.many_to_many: + setattr(instance, f.attname, getattr(instance, f.attname).model.objects.filter(pk__in = 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 + # exception in that case. return instance def make_instance_save(instance): |
