mirror of
				https://github.com/django/django.git
				synced 2025-10-31 01:25:32 +00:00 
			
		
		
		
	Fixed #32807 -- Fixed JSONField crash when redisplaying None values.
Thanks to Alex Hill for the initial patch.
This commit is contained in:
		
				
					committed by
					
						 Mariusz Felisiak
						Mariusz Felisiak
					
				
			
			
				
	
			
			
			
						parent
						
							66af94d56e
						
					
				
				
					commit
					f5ea9aa2f3
				
			| @@ -1251,6 +1251,8 @@ class JSONField(CharField): | ||||
|     def bound_data(self, data, initial): | ||||
|         if self.disabled: | ||||
|             return initial | ||||
|         if data is None: | ||||
|             return None | ||||
|         try: | ||||
|             return json.loads(data, cls=self.decoder) | ||||
|         except json.JSONDecodeError: | ||||
|   | ||||
| @@ -97,6 +97,21 @@ class JSONFieldTest(SimpleTestCase): | ||||
|         form = JSONForm({'json_field': '["bar"]'}, initial={'json_field': ['foo']}) | ||||
|         self.assertIn('["foo"]</textarea>', form.as_p()) | ||||
|  | ||||
|     def test_redisplay_none_input(self): | ||||
|         class JSONForm(Form): | ||||
|             json_field = JSONField(required=True) | ||||
|  | ||||
|         tests = [ | ||||
|             {}, | ||||
|             {'json_field': None}, | ||||
|         ] | ||||
|         for data in tests: | ||||
|             with self.subTest(data=data): | ||||
|                 form = JSONForm(data) | ||||
|                 self.assertEqual(form['json_field'].value(), 'null') | ||||
|                 self.assertIn('null</textarea>', form.as_p()) | ||||
|                 self.assertEqual(form.errors['json_field'], ['This field is required.']) | ||||
|  | ||||
|     def test_redisplay_wrong_input(self): | ||||
|         """ | ||||
|         Displaying a bound form (typically due to invalid input). The form | ||||
|   | ||||
		Reference in New Issue
	
	Block a user