Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Pass exclude into save_instance and honor it. #413

Closed
wants to merge 1 commit into from

3 participants

@melinath

Added test for field exclusion.

Corrections to test.

Ticket on trac

@charettes
Collaborator

This is a PR for ticket #12337.

@melinath melinath Pass exclude into save_instance and honor it.
Added test for field exclusion.

Corrections to test.
1acd5ae
@timgraham
Owner

merged in e2518fd, thanks!

@timgraham timgraham closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 3, 2012
  1. @melinath

    Pass exclude into save_instance and honor it.

    melinath authored
    Added test for field exclusion.
    
    Corrections to test.
This page is out of date. Refresh to see the latest.
View
5 django/forms/models.py
@@ -80,6 +80,8 @@ def save_m2m():
for f in opts.many_to_many:
if fields and f.name not in fields:
continue
+ if exclude and f.name in exclude:
+ continue
if f.name in cleaned_data:
f.save_form_data(instance, cleaned_data[f.name])
if commit:
@@ -362,7 +364,8 @@ def save(self, commit=True):
else:
fail_message = 'changed'
return save_instance(self, self.instance, self._meta.fields,
- fail_message, commit, construct=False)
+ fail_message, commit, self._meta.exclude,
+ construct=False)
save.alters_data = True
View
2  tests/regressiontests/forms/tests/__init__.py
@@ -13,7 +13,7 @@
CustomDateTimeInputFormatsTests, SimpleDateTimeFormatTests)
from .media import FormsMediaTestCase, StaticFormsMediaTestCase
from .models import (TestTicket12510, ModelFormCallableModelDefault,
- FormsModelTestCase, RelatedModelFormTests)
+ FormsModelTestCase, RelatedModelFormTests, ManyToManyExclusionTestCase)
from .regressions import FormsRegressionsTestCase
from .util import FormsUtilTestCase
from .validators import TestFieldWithValidators
View
38 tests/regressiontests/forms/tests/models.py
@@ -5,7 +5,7 @@
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db import models
-from django.forms import Form, ModelForm, FileField, ModelChoiceField
+from django.forms import Form, ModelForm, FileField, ModelChoiceField, CharField
from django.forms.models import ModelFormMetaclass
from django.test import TestCase
from django.utils import six
@@ -24,6 +24,14 @@ class Meta:
model = OptionalMultiChoiceModel
+class ChoiceFieldExclusionForm(ModelForm):
+ multi_choice = CharField(max_length=50)
+
+ class Meta:
+ exclude = ['multi_choice']
+ model = ChoiceFieldModel
+
+
class FileForm(Form):
file1 = FileField()
@@ -216,3 +224,31 @@ class Meta:
model=A
self.assertTrue(issubclass(ModelFormMetaclass(str('Form'), (ModelForm,), {'Meta': Meta}), ModelForm))
+
+
+class ManyToManyExclusionTestCase(TestCase):
+ def test_m2m_field_exclusion(self):
+ # Issue 12337. save_instance should honor the passed-in exclude keyword.
+ opt1 = ChoiceOptionModel.objects.create(id=1, name='default')
+ opt2 = ChoiceOptionModel.objects.create(id=2, name='option 2')
+ opt3 = ChoiceOptionModel.objects.create(id=3, name='option 3')
+ initial = {
+ 'choice': opt1,
+ 'choice_int': opt1,
+ }
+ data = {
+ 'choice': opt2.pk,
+ 'choice_int': opt2.pk,
+ 'multi_choice': 'string data!',
+ 'multi_choice_int': [opt1.pk],
+ }
+ instance = ChoiceFieldModel.objects.create(**initial)
+ instance.multi_choice = instance.multi_choice_int = [opt2, opt3]
+ form = ChoiceFieldExclusionForm(data=data, instance=instance)
+ self.assertTrue(form.is_valid())
+ self.assertEqual(form.cleaned_data['multi_choice'], data['multi_choice'])
+ form.save()
+ self.assertEqual(form.instance.choice.pk, data['choice'])
+ self.assertEqual(form.instance.choice_int.pk, data['choice_int'])
+ self.assertEqual(list(form.instance.multi_choice.all()), [opt2, opt3])
+ self.assertEqual([obj.pk for obj in form.instance.multi_choice_int.all()], data['multi_choice_int'])
Something went wrong with that request. Please try again.