mirror of
				https://github.com/django/django.git
				synced 2025-10-30 17:16:10 +00:00 
			
		
		
		
	Fixed #25942 -- Fixed TypedChoiceField.has_changed with nullable field
This fixes a regression introduced by 871440361.
			
			
This commit is contained in:
		| @@ -181,17 +181,16 @@ class Field(object): | |||||||
|         """ |         """ | ||||||
|         Return True if data differs from initial. |         Return True if data differs from initial. | ||||||
|         """ |         """ | ||||||
|         # For purposes of seeing whether something has changed, None is |  | ||||||
|         # the same as an empty string, if the data or initial value we get |  | ||||||
|         # is None, replace it w/ ''. |  | ||||||
|         initial_value = initial if initial is not None else '' |  | ||||||
|         try: |         try: | ||||||
|             data = self.to_python(data) |             data = self.to_python(data) | ||||||
|             if hasattr(self, '_coerce'): |             if hasattr(self, '_coerce'): | ||||||
|                 data = self._coerce(data) |                 return self._coerce(data) != self._coerce(initial) | ||||||
|                 initial_value = self._coerce(initial_value) |  | ||||||
|         except ValidationError: |         except ValidationError: | ||||||
|             return True |             return True | ||||||
|  |         # For purposes of seeing whether something has changed, None is | ||||||
|  |         # the same as an empty string, if the data or initial value we get | ||||||
|  |         # is None, replace it with ''. | ||||||
|  |         initial_value = initial if initial is not None else '' | ||||||
|         data_value = data if data is not None else '' |         data_value = data if data is not None else '' | ||||||
|         return initial_value != data_value |         return initial_value != data_value | ||||||
|  |  | ||||||
|   | |||||||
| @@ -43,3 +43,6 @@ Bugfixes | |||||||
|  |  | ||||||
| * Fixed a state bug when using an ``AlterModelManagers`` operation | * Fixed a state bug when using an ``AlterModelManagers`` operation | ||||||
|   (:ticket:`25852`). |   (:ticket:`25852`). | ||||||
|  |  | ||||||
|  | * Fixed ``TypedChoiceField`` change detection with nullable fields | ||||||
|  |   (:ticket:`25942`). | ||||||
|   | |||||||
| @@ -1246,6 +1246,14 @@ class FieldsTests(SimpleTestCase): | |||||||
|         self.assertFalse(f.has_changed(1, '1')) |         self.assertFalse(f.has_changed(1, '1')) | ||||||
|         self.assertFalse(f.has_changed('1', '1')) |         self.assertFalse(f.has_changed('1', '1')) | ||||||
|  |  | ||||||
|  |         f = TypedChoiceField( | ||||||
|  |             choices=[('', '---------'), ('a', "a"), ('b', "b")], coerce=six.text_type, | ||||||
|  |             required=False, initial=None, empty_value=None, | ||||||
|  |         ) | ||||||
|  |         self.assertFalse(f.has_changed(None, '')) | ||||||
|  |         self.assertTrue(f.has_changed('', 'a')) | ||||||
|  |         self.assertFalse(f.has_changed('a', 'a')) | ||||||
|  |  | ||||||
|     def test_typedchoicefield_special_coerce(self): |     def test_typedchoicefield_special_coerce(self): | ||||||
|         """ |         """ | ||||||
|         Test a coerce function which results in a value not present in choices. |         Test a coerce function which results in a value not present in choices. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user