From 3f79909a4299fa34c85b007966cee03563faedf2 Mon Sep 17 00:00:00 2001 From: Boris Shemigon Date: Sat, 19 Oct 2024 15:36:26 -0400 Subject: [PATCH 1/4] handle "instance" field name --- model_utils/tracker.py | 6 +++--- tests/models.py | 7 +++++++ .../test_special_name_field_model.py | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 tests/test_models/test_special_name_field_model.py diff --git a/model_utils/tracker.py b/model_utils/tracker.py index 61093802..09282b5a 100644 --- a/model_utils/tracker.py +++ b/model_utils/tracker.py @@ -398,9 +398,9 @@ def patch_init(self, model: type[models.Model]) -> None: original = getattr(model, '__init__') @wraps(original) - def inner(instance: models.Model, *args: Any, **kwargs: Any) -> None: - original(instance, *args, **kwargs) - self.initialize_tracker(model, instance) + def inner(*args: Any, **kwargs: Any) -> None: + original(*args, **kwargs) + self.initialize_tracker(model, args[0]) setattr(model, '__init__', inner) diff --git a/tests/models.py b/tests/models.py index 4d345050..faa4900e 100644 --- a/tests/models.py +++ b/tests/models.py @@ -326,6 +326,13 @@ class ModelTrackedMultiple(models.Model): number_tracker = ModelTracker(fields=['number']) +class TrackedModelWithSpecialNamedField(models.Model): + instance = models.CharField(max_length=20) + name = models.CharField(max_length=20) + + tracker = FieldTracker() + + class InheritedModelTracked(ModelTracked): name2 = models.CharField(max_length=20) diff --git a/tests/test_models/test_special_name_field_model.py b/tests/test_models/test_special_name_field_model.py new file mode 100644 index 00000000..7a0e693a --- /dev/null +++ b/tests/test_models/test_special_name_field_model.py @@ -0,0 +1,19 @@ +from __future__ import annotations + +from django.test import TestCase + +from tests.models import TrackedModelWithSpecialNamedField + + +class SpecialNamedFieldTests(TestCase): + def test_model_with_instance_field(self) -> None: + t = TrackedModelWithSpecialNamedField.objects.create( + instance="45.55", + name="test", + ) + self.assertEqual(t.instance, "45.55") + + t.instance = "56.78" + t.save() + + t.delete() From f2eaa4967802793af91180732d1888cbdaa3e0e2 Mon Sep 17 00:00:00 2001 From: Boris Shemigon Date: Sat, 19 Oct 2024 15:37:27 -0400 Subject: [PATCH 2/4] fix timezone warnings in tests --- tests/test_fields/test_field_tracker.py | 5 ++++- tests/test_models/test_timeframed_model.py | 5 +++-- tests/test_models/test_timestamped_model.py | 13 +++++++------ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/tests/test_fields/test_field_tracker.py b/tests/test_fields/test_field_tracker.py index 81db1ec4..44982512 100644 --- a/tests/test_fields/test_field_tracker.py +++ b/tests/test_fields/test_field_tracker.py @@ -121,7 +121,10 @@ def test_pre_save_has_changed(self) -> None: def test_save_with_args(self) -> None: self.instance.number = 1 - self.instance.save(False, False, None, None) + self.instance.save(force_insert=False, + force_update=False, + using=None, + update_fields=None) self.assertChanged() def test_first_save(self) -> None: diff --git a/tests/test_models/test_timeframed_model.py b/tests/test_models/test_timeframed_model.py index 246b3992..91a73ed0 100644 --- a/tests/test_models/test_timeframed_model.py +++ b/tests/test_models/test_timeframed_model.py @@ -1,10 +1,11 @@ from __future__ import annotations -from datetime import datetime, timedelta +from datetime import timedelta from django.core.exceptions import ImproperlyConfigured from django.db import models from django.test import TestCase +from django.utils.timezone import now from model_utils.managers import QueryManager from model_utils.models import TimeFramedModel @@ -13,7 +14,7 @@ class TimeFramedModelTests(TestCase): def setUp(self) -> None: - self.now = datetime.now() + self.now = now() def test_not_yet_begun(self) -> None: TimeFrame.objects.create(start=self.now + timedelta(days=2)) diff --git a/tests/test_models/test_timestamped_model.py b/tests/test_models/test_timestamped_model.py index 66979417..fe32caaa 100644 --- a/tests/test_models/test_timestamped_model.py +++ b/tests/test_models/test_timestamped_model.py @@ -5,6 +5,7 @@ import time_machine from django.test import TestCase +from django.utils.timezone import now from tests.models import TimeStamp, TimeStampWithStatusModel @@ -36,7 +37,7 @@ def test_overriding_created_via_object_creation_also_uses_creation_date_for_modi Setting the created date when first creating an object should be permissible. """ - different_date = datetime.today() - timedelta(weeks=52) + different_date = now() - timedelta(weeks=52) t1 = TimeStamp.objects.create(created=different_date) self.assertEqual(t1.created, different_date) self.assertEqual(t1.modified, different_date) @@ -46,7 +47,7 @@ def test_overriding_modified_via_object_creation(self) -> None: Setting the modified date explicitly should be possible when first creating an object, but not thereafter. """ - different_date = datetime.today() - timedelta(weeks=52) + different_date = now() - timedelta(weeks=52) t1 = TimeStamp.objects.create(modified=different_date) self.assertEqual(t1.modified, different_date) self.assertNotEqual(t1.created, different_date) @@ -56,7 +57,7 @@ def test_overriding_created_after_object_created(self) -> None: The created date may be changed post-create """ t1 = TimeStamp.objects.create() - different_date = datetime.today() - timedelta(weeks=52) + different_date = now() - timedelta(weeks=52) t1.created = different_date t1.save() self.assertEqual(t1.created, different_date) @@ -67,7 +68,7 @@ def test_overriding_modified_after_object_created(self) -> None: is saved, regardless of attempts to change it. """ t1 = TimeStamp.objects.create() - different_date = datetime.today() - timedelta(weeks=52) + different_date = now() - timedelta(weeks=52) t1.modified = different_date t1.save() self.assertNotEqual(t1.modified, different_date) @@ -79,13 +80,13 @@ def test_overrides_using_save(self) -> None: After that, only created may be modified manually. """ t1 = TimeStamp() - different_date = datetime.today() - timedelta(weeks=52) + different_date = now() - timedelta(weeks=52) t1.created = different_date t1.modified = different_date t1.save() self.assertEqual(t1.created, different_date) self.assertEqual(t1.modified, different_date) - different_date2 = datetime.today() - timedelta(weeks=26) + different_date2 = now() - timedelta(weeks=26) t1.created = different_date2 t1.modified = different_date2 t1.save() From c1cd2e60c234dbf35823e82c74fa10096cf06dcb Mon Sep 17 00:00:00 2001 From: Boris Shemigon Date: Sat, 19 Oct 2024 15:38:03 -0400 Subject: [PATCH 3/4] upgrade postgres to v14 to get tests pass with latest django --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 6e74ec82..5299dc3d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ services: postgres: - image: postgres:13-alpine + image: postgres:14-alpine environment: POSTGRES_HOST_AUTH_METHOD: trust POSTGRES_DB: modelutils From cf847c48bd8c189ed42870ece8227e4ed83e9130 Mon Sep 17 00:00:00 2001 From: Boris Shemigon Date: Sat, 19 Oct 2024 15:38:14 -0400 Subject: [PATCH 4/4] authors update --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index b601622d..2e37ed11 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -106,3 +106,4 @@ | Őry Máté | Nafees Anwar | meanmail +| Boris Shemigon