Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add historic one to one field #1265

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 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,23 @@ 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
Loading