Skip to content

Commit 7d7f74b

Browse files
committed
get previous value
1 parent f021d88 commit 7d7f74b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

codeforlife/models/signals/post_save.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from . import general as _
1212
from .pre_save import PREVIOUS_VALUE_KEY
1313

14+
FieldValue = t.TypeVar("FieldValue")
15+
1416

1517
def has_previous_values(instance: _.AnyModel, fields: t.Dict[str, t.Type]):
1618
# pylint: disable=line-too-long
@@ -36,3 +38,27 @@ def has_previous_values(instance: _.AnyModel, fields: t.Dict[str, t.Type]):
3638
return False
3739

3840
return True
41+
42+
43+
def get_previous_value(
44+
instance: _.AnyModel, field: str, cls: t.Type[FieldValue]
45+
):
46+
# pylint: disable=line-too-long
47+
"""Get a previous value from the instance and assert the value is of the
48+
expected type.
49+
50+
Args:
51+
instance: The current instance.
52+
field: The field to get the previous value for.
53+
cls: The expected type of the value.
54+
55+
Returns:
56+
The previous value of the field.
57+
"""
58+
# pylint: enable=line-too-long
59+
60+
previous_value = getattr(instance, PREVIOUS_VALUE_KEY.format(field=field))
61+
62+
assert isinstance(previous_value, cls)
63+
64+
return previous_value

0 commit comments

Comments
 (0)