Skip to content

Commit

Permalink
add historic one to one field
Browse files Browse the repository at this point in the history
  • Loading branch information
david-homelend committed Sep 28, 2023
1 parent fd9aa52 commit 3e763c3
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 = {}


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e763c3

Please sign in to comment.