mirror of
				https://github.com/django/django.git
				synced 2025-10-30 17:16:10 +00:00 
			
		
		
		
	Fixed #31863 -- Prevented mutating model state by copies of model instances.
Regression in bfb746f983.
			
			
This commit is contained in:
		
				
					committed by
					
						 Mariusz Felisiak
						Mariusz Felisiak
					
				
			
			
				
	
			
			
			
						parent
						
							63300f7e68
						
					
				
				
					commit
					94ea79be13
				
			| @@ -546,7 +546,10 @@ class Model(metaclass=ModelBase): | |||||||
|  |  | ||||||
|     def __getstate__(self): |     def __getstate__(self): | ||||||
|         """Hook to allow choosing the attributes to pickle.""" |         """Hook to allow choosing the attributes to pickle.""" | ||||||
|         return self.__dict__ |         state = self.__dict__.copy() | ||||||
|  |         state['_state'] = copy.copy(state['_state']) | ||||||
|  |         state['_state'].fields_cache = state['_state'].fields_cache.copy() | ||||||
|  |         return state | ||||||
|  |  | ||||||
|     def __setstate__(self, state): |     def __setstate__(self, state): | ||||||
|         pickled_version = state.get(DJANGO_VERSION_PICKLE_KEY) |         pickled_version = state.get(DJANGO_VERSION_PICKLE_KEY) | ||||||
|   | |||||||
| @@ -1,3 +1,4 @@ | |||||||
|  | import copy | ||||||
| import datetime | import datetime | ||||||
| from operator import attrgetter | from operator import attrgetter | ||||||
|  |  | ||||||
| @@ -256,3 +257,17 @@ class EvaluateMethodTest(TestCase): | |||||||
|         dept = Department.objects.create(pk=1, name='abc') |         dept = Department.objects.create(pk=1, name='abc') | ||||||
|         dept.evaluate = 'abc' |         dept.evaluate = 'abc' | ||||||
|         Worker.objects.filter(department=dept) |         Worker.objects.filter(department=dept) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class ModelFieldsCacheTest(TestCase): | ||||||
|  |     def test_fields_cache_reset_on_copy(self): | ||||||
|  |         department1 = Department.objects.create(id=1, name='department1') | ||||||
|  |         department2 = Department.objects.create(id=2, name='department2') | ||||||
|  |         worker1 = Worker.objects.create(name='worker', department=department1) | ||||||
|  |         worker2 = copy.copy(worker1) | ||||||
|  |  | ||||||
|  |         self.assertEqual(worker2.department, department1) | ||||||
|  |         # Changing related fields doesn't mutate the base object. | ||||||
|  |         worker2.department = department2 | ||||||
|  |         self.assertEqual(worker2.department, department2) | ||||||
|  |         self.assertEqual(worker1.department, department1) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user