Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #18898 -- Added tests with a fix for ModelMultipleChoiceField

  • Loading branch information...
commit 3318595c0bfeda9f6bae8aa11dda68647ae55fde 1 parent 892bc91
Claude Paroz authored January 26, 2013
2  django/forms/models.py
@@ -1072,6 +1072,6 @@ def _has_changed(self, initial, data):
1072 1072
             data = []
1073 1073
         if len(initial) != len(data):
1074 1074
             return True
1075  
-        initial_set = set([force_text(value) for value in initial])
  1075
+        initial_set = set([force_text(value) for value in self.prepare_value(initial)])
1076 1076
         data_set = set([force_text(value) for value in data])
1077 1077
         return data_set != initial_set
32  tests/model_formsets/tests.py
@@ -1071,6 +1071,38 @@ def test_model_formset_with_custom_pk(self):
1071 1071
         FormSet = modelformset_factory(ClassyMexicanRestaurant, fields=["tacos_are_yummy"])
1072 1072
         self.assertEqual(sorted(FormSet().forms[0].fields.keys()), ['restaurant', 'tacos_are_yummy'])
1073 1073
 
  1074
+    def test_model_formset_with_initial_model_instance(self):
  1075
+        # has_changed should compare model instance and primary key
  1076
+        # see #18898
  1077
+        FormSet = modelformset_factory(Poem)
  1078
+        john_milton = Poet(name="John Milton")
  1079
+        john_milton.save()
  1080
+        data = {
  1081
+            'form-TOTAL_FORMS': 1,
  1082
+            'form-INITIAL_FORMS': 0,
  1083
+            'form-MAX_NUM_FORMS': '',
  1084
+            'form-0-name': '',
  1085
+            'form-0-poet': str(john_milton.id),
  1086
+        }
  1087
+        formset = FormSet(initial=[{'poet': john_milton}], data=data)
  1088
+        self.assertFalse(formset.extra_forms[0].has_changed())
  1089
+
  1090
+    def test_model_formset_with_initial_queryset(self):
  1091
+        # has_changed should work with queryset and list of pk's
  1092
+        # see #18898
  1093
+        FormSet = modelformset_factory(AuthorMeeting)
  1094
+        author = Author.objects.create(pk=1, name='Charles Baudelaire')
  1095
+        data = {
  1096
+            'form-TOTAL_FORMS': 1,
  1097
+            'form-INITIAL_FORMS': 0,
  1098
+            'form-MAX_NUM_FORMS': '',
  1099
+            'form-0-name': '',
  1100
+            'form-0-created': '',
  1101
+            'form-0-authors': list(Author.objects.values_list('id', flat=True)),
  1102
+        }
  1103
+        formset = FormSet(initial=[{'authors': Author.objects.all()}], data=data)
  1104
+        self.assertFalse(formset.extra_forms[0].has_changed())
  1105
+
1074 1106
     def test_prevent_duplicates_from_with_the_same_formset(self):
1075 1107
         FormSet = modelformset_factory(Product, extra=2)
1076 1108
         data = {

0 notes on commit 3318595

Please sign in to comment.
Something went wrong with that request. Please try again.