From 3ef6da052da06873572e2f68f132d6cd4a630e1d Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Fri, 26 Apr 2024 19:50:28 +0200 Subject: [PATCH] Fixed problem when setting `django.db.models.functions.Now()` in `DateTimeField` --- CHANGELOG.md | 4 ++++ auditlog/diff.py | 2 ++ auditlog_tests/tests.py | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 299414fe..5731b850 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Next Release +#### Fixes + +- Fixed problem when setting `django.db.models.functions.Now()` in `DateTimeField` ([#624](https://github.com/jazzband/django-auditlog/pull/624)) + ## 3.0.0 (2024-04-12) #### Fixes diff --git a/auditlog/diff.py b/auditlog/diff.py index b4294fed..663b8dce 100644 --- a/auditlog/diff.py +++ b/auditlog/diff.py @@ -90,6 +90,8 @@ def get_field_value(obj, field): if getattr(field, "default", NOT_PROVIDED) is not NOT_PROVIDED else None ) + except TypeError: + pass return value diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index 627324f4..a93989b9 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -17,6 +17,7 @@ from django.contrib.contenttypes.models import ContentType from django.core import management from django.db import models +from django.db.models.functions import Now from django.db.models.signals import pre_save from django.test import RequestFactory, TestCase, override_settings from django.urls import resolve, reverse @@ -1071,6 +1072,24 @@ def test_update_naive_dt(self): ) dtm.save() + def test_datetime_field_functions_now(self): + timestamp = datetime.datetime(2017, 1, 10, 15, 0, tzinfo=timezone.utc) + date = datetime.date(2017, 1, 10) + time = datetime.time(12, 0) + + dtm = DateTimeFieldModel( + label="DateTimeField model", + timestamp=timestamp, + date=date, + time=time, + naive_dt=Now(), + ) + dtm.save() + dtm.naive_dt = Now() + self.assertEqual(dtm.naive_dt, Now()) + dtm.save() + self.assertEqual(dtm.naive_dt, Now()) + class UnregisterTest(TestCase): def setUp(self):