mirror of
				https://github.com/django/django.git
				synced 2025-10-30 09:06:13 +00:00 
			
		
		
		
	Fixed #21102 -- pickling a QuerySet with prefetches twice
Fixed the bug that a QuerySet that prefetches related objects cannot be
pickled and unpickled more than once (The second pickling attempt
raises an exception).
Added a new test for the queryset pickling idempotency.
The bug was introduced by
bac187c0d8.
			
			
This commit is contained in:
		
				
					committed by
					
						 Anssi Kääriäinen
						Anssi Kääriäinen
					
				
			
			
				
	
			
			
			
						parent
						
							dbc2e8eb73
						
					
				
				
					commit
					e66fe357b2
				
			| @@ -80,10 +80,10 @@ class QuerySet(object): | |||||||
|         if model is None: |         if model is None: | ||||||
|             # if model is None, then self should be emptyqs and the related |             # if model is None, then self should be emptyqs and the related | ||||||
|             # objects do not matter. |             # objects do not matter. | ||||||
|             self._known_related_objects = {} |             obj_dict['_known_related_objects'] = {} | ||||||
|         else: |         else: | ||||||
|             opts = model._meta |             opts = model._meta | ||||||
|             self._known_related_objects = dict( |             obj_dict['_known_related_objects'] = dict( | ||||||
|                 (opts.get_field(field.name if hasattr(field, 'name') else field), val) |                 (opts.get_field(field.name if hasattr(field, 'name') else field), val) | ||||||
|                 for field, val in obj_dict['_known_related_objects'].items() |                 for field, val in obj_dict['_known_related_objects'].items() | ||||||
|             ) |             ) | ||||||
|   | |||||||
| @@ -73,3 +73,15 @@ class PickleabilityTestCase(TestCase): | |||||||
|         empty = pickle.loads(dumped) |         empty = pickle.loads(dumped) | ||||||
|         self.assertQuerysetEqual( |         self.assertQuerysetEqual( | ||||||
|             empty, []) |             empty, []) | ||||||
|  |  | ||||||
|  |     def test_pickle_prefetch_related_idempotence(self): | ||||||
|  |         p = Post.objects.create() | ||||||
|  |         posts = Post.objects.prefetch_related('materials') | ||||||
|  |  | ||||||
|  |         # First pickling | ||||||
|  |         posts = pickle.loads(pickle.dumps(posts)) | ||||||
|  |         self.assertQuerysetEqual(posts, [p], lambda x: x) | ||||||
|  |  | ||||||
|  |         # Second pickling | ||||||
|  |         posts = pickle.loads(pickle.dumps(posts)) | ||||||
|  |         self.assertQuerysetEqual(posts, [p], lambda x: x) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user