From 3e763c370fe50b594dd012917ea75fd0184bd69f Mon Sep 17 00:00:00 2001 From: David Diamant Date: Thu, 28 Sep 2023 14:28:23 +0300 Subject: [PATCH 1/2] add historic one to one field --- simple_history/models.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/simple_history/models.py b/simple_history/models.py index 6dc4db9e8..80eb06ba9 100644 --- a/simple_history/models.py +++ b/simple_history/models.py @@ -3,6 +3,7 @@ import uuid import warnings from functools import partial +from typing import TypeVar from django.apps import apps from django.conf import settings @@ -15,7 +16,9 @@ from django.db.models.fields.related import ForeignKey from django.db.models.fields.related_descriptors import ( ForwardManyToOneDescriptor, + ForwardOneToOneDescriptor, ReverseManyToOneDescriptor, + ReverseOneToOneDescriptor, create_reverse_many_to_one_manager, ) from django.db.models.query import QuerySet @@ -45,6 +48,13 @@ except ImportError: from threading import local as LocalContext + +# __set__ value type +_ST = TypeVar("_ST") +# __get__ return type +_GT = TypeVar("_GT") + + registered_models = {} @@ -909,6 +919,21 @@ def to_historic(instance): return getattr(instance, SIMPLE_HISTORY_REVERSE_ATTR_NAME, None) +class HistoricForwardOneToOneDescriptor(ForwardOneToOneDescriptor, + HistoricForwardManyToOneDescriptor): + pass + + +class HistoricReverseOneToOneDescriptor(ReverseOneToOneDescriptor, + HistoricReverseManyToOneDescriptor): + pass + + +class HistoricOneToOneField(models.OneToOneField[_ST, _GT]): + forward_related_accessor_class = HistoricForwardOneToOneDescriptor + related_accessor_class = HistoricReverseOneToOneDescriptor + + class HistoricalObjectDescriptor: def __init__(self, model, fields_included): self.model = model From 5091489c9a140738a746c12d569bd729468c39ba Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 28 Sep 2023 11:29:04 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- simple_history/models.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/simple_history/models.py b/simple_history/models.py index 80eb06ba9..41a6f158e 100644 --- a/simple_history/models.py +++ b/simple_history/models.py @@ -919,13 +919,15 @@ def to_historic(instance): return getattr(instance, SIMPLE_HISTORY_REVERSE_ATTR_NAME, None) -class HistoricForwardOneToOneDescriptor(ForwardOneToOneDescriptor, - HistoricForwardManyToOneDescriptor): +class HistoricForwardOneToOneDescriptor( + ForwardOneToOneDescriptor, HistoricForwardManyToOneDescriptor +): pass -class HistoricReverseOneToOneDescriptor(ReverseOneToOneDescriptor, - HistoricReverseManyToOneDescriptor): +class HistoricReverseOneToOneDescriptor( + ReverseOneToOneDescriptor, HistoricReverseManyToOneDescriptor +): pass