From ef3bbc33c3087448a82e29c5468b9cfb4adff1b3 Mon Sep 17 00:00:00 2001 From: MartinGotelli Date: Fri, 19 Apr 2024 10:32:04 -0300 Subject: [PATCH] #173: Use attname instead of name when assigning the attributes values on the mocked _do_update --- django_mock_queries/mocks.py | 2 +- tests/test_mocks.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/django_mock_queries/mocks.py b/django_mock_queries/mocks.py index a246f4e..82d71eb 100644 --- a/django_mock_queries/mocks.py +++ b/django_mock_queries/mocks.py @@ -476,7 +476,7 @@ def _do_update(self, *args, **_): objects = self.objects.filter(pk=pk_val) if objects.exists(): - attrs = {field.name: value for field, _, value in values if value is not None} + attrs = {field.attname: value for field, _, value in values if value is not None} self.objects.update(**attrs) return True else: diff --git a/tests/test_mocks.py b/tests/test_mocks.py index ce78b4b..5505c85 100644 --- a/tests/test_mocks.py +++ b/tests/test_mocks.py @@ -435,6 +435,16 @@ def test_model_mocker_objects_create(self): obj = Car.objects.create(speed=10) self.assertEqual(Car.objects.get(pk=obj.id), obj) + def test_model_mocker_update_fk_from_instance(self): + with ModelMocker(Manufacturer): + with ModelMocker(Car, outer=False): + manufacturer = Manufacturer.objects.create(name='foo') + obj = Car.objects.create(speed=10, make=manufacturer) + obj.make = Manufacturer.objects.create(name='bar') + obj.save() + + self.assertEqual(Car.objects.get(pk=obj.id).make.name, 'bar') + def test_model_mocker_with_custom_method(self): with self.CarModelMocker(Car, 'validate_price') as mocker: obj = Car()