mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	[1.10.x] Refs #24227 -- Fixed crash of ManyToManyField.value_from_object() on unsaved model instances.
This behavior was removed in67d984413cbut is needed to prevent a crash in formtools. Backport ofa4c20ae85bfrom master
This commit is contained in:
		| @@ -1565,6 +1565,8 @@ class ManyToManyField(RelatedField): | |||||||
|         """ |         """ | ||||||
|         Return the value of this field in the given model instance. |         Return the value of this field in the given model instance. | ||||||
|         """ |         """ | ||||||
|  |         if obj.pk is None: | ||||||
|  |             return [] | ||||||
|         qs = getattr(obj, self.attname).all() |         qs = getattr(obj, self.attname).all() | ||||||
|         if qs._result_cache is not None: |         if qs._result_cache is not None: | ||||||
|             return [item.pk for item in qs] |             return [item.pk for item in qs] | ||||||
|   | |||||||
| @@ -6,6 +6,14 @@ from django.test.utils import isolate_apps | |||||||
|  |  | ||||||
| class ManyToManyFieldTests(SimpleTestCase): | class ManyToManyFieldTests(SimpleTestCase): | ||||||
|  |  | ||||||
|  |     @isolate_apps('model_fields') | ||||||
|  |     def test_value_from_object_instance_without_pk(self): | ||||||
|  |         class ManyToManyModel(models.Model): | ||||||
|  |             m2m = models.ManyToManyField('self', models.CASCADE) | ||||||
|  |  | ||||||
|  |         instance = ManyToManyModel() | ||||||
|  |         self.assertEqual(instance._meta.get_field('m2m').value_from_object(instance), []) | ||||||
|  |  | ||||||
|     def test_abstract_model_pending_operations(self): |     def test_abstract_model_pending_operations(self): | ||||||
|         """ |         """ | ||||||
|         Many-to-many fields declared on abstract models should not add lazy |         Many-to-many fields declared on abstract models should not add lazy | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user