MCPcopy
hub / github.com/tortoise/tortoise-orm / to_db_value

Method to_db_value

tortoise/fields/data.py:484–507  ·  view source on GitHub ↗
(
        self, value: DatetimeFieldQueryValueType | None, instance: type[Model] | Model
    )

Source from the content-addressed store, hash-verified

482 return value
483
484 def to_db_value(
485 self, value: DatetimeFieldQueryValueType | None, instance: type[Model] | Model
486 ) -> DatetimeFieldQueryValueType | None:
487 # Only do this if it is a Model instance, not class. Test for guaranteed instance var
488 if hasattr(instance, "_saved_in_db") and (
489 self.auto_now
490 or (self.auto_now_add and getattr(instance, self.model_field_name) is None)
491 ):
492 now = timezone.now()
493 # Convert to match what would be read from DB (apply timezone conversion)
494 now_python = self.to_python_value(now)
495 setattr(instance, self.model_field_name, now_python)
496 return now # type:ignore[return-value]
497 if value is not None:
498 if isinstance(value, datetime.datetime) and get_use_tz():
499 if timezone.is_naive(value):
500 warnings.warn(
501 f"DateTimeField {self.model_field_name} received a naive datetime ({value})"
502 " while time zone support is active.",
503 RuntimeWarning,
504 )
505 value = timezone.make_aware(value, "UTC")
506 self.validate(value)
507 return value
508
509 @property
510 def constraints(self) -> dict:

Callers

nothing calls this directly

Calls 3

to_python_valueMethod · 0.95
get_use_tzFunction · 0.90
validateMethod · 0.80

Tested by

no test coverage detected