Skip to content

Commit

Permalink
Give extract_field_value() the ability to detect and handle transform…
Browse files Browse the repository at this point in the history
… expressions for date, time and datetime field values
  • Loading branch information
ababic committed Feb 17, 2024
1 parent c8f4ea5 commit 4034c26
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions modelcluster/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
from functools import lru_cache
from django.core.exceptions import FieldDoesNotExist
from django.db.models import (
Expand Down Expand Up @@ -141,7 +142,22 @@ def extract_field_value(obj, key, pk_only=False, suppress_fielddoesnotexist=Fals
latest_obj = obj
segments = key.split(REL_DELIMETER)
for i, segment in enumerate(segments, start=1):
if hasattr(source, segment):
if (
isinstance(source, datetime.date)
and segment in datetime_utils.DATEFIELD_TRANSFORM_EXPRESSIONS
):
source = datetime_utils.derive_from_date(source, segment)
elif (
isinstance(source, datetime.time)
and segment in datetime_utils.TIMEFIELD_TRANSFORM_EXPRESSIONS
):
source = datetime_utils.derive_from_time(source, segment)
elif (
isinstance(source, datetime.datetime)
and segment in datetime_utils.DATETIMEFIELD_TRANSFORM_EXPRESSIONS
):
source = datetime_utils.derive_from_datetime(source, segment)
elif hasattr(source, segment):
value = getattr(source, segment)
if isinstance(value, Model):
latest_obj = value
Expand All @@ -158,7 +174,6 @@ def extract_field_value(obj, key, pk_only=False, suppress_fielddoesnotexist=Fals
)
)
source = value
continue
elif suppress_fielddoesnotexist:
return None
else:
Expand Down

0 comments on commit 4034c26

Please sign in to comment.