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 84be1af commit 2e5491f
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 @@ -138,7 +139,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 @@ -155,7 +171,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 2e5491f

Please sign in to comment.