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

Class DatabaseDefault

tortoise/fields/base.py:44–73  ·  view source on GitHub ↗

Sentinel indicating that the database should apply its default value. When a field has ``db_default`` and the user does not provide a value, this object is set as the attribute value on the model instance. During INSERT compilation it is detected via ``isinstance()`` checks: - Sing

Source from the content-addressed store, hash-verified

42
43
44class DatabaseDefault:
45 """Sentinel indicating that the database should apply its default value.
46
47 When a field has ``db_default`` and the user does not provide a value,
48 this object is set as the attribute value on the model instance.
49
50 During INSERT compilation it is detected via ``isinstance()`` checks:
51 - Single-insert path: columns with DatabaseDefault are omitted from the
52 INSERT statement, so the DB applies its DEFAULT.
53 - Bulk-insert path: columns where *all* instances hold DatabaseDefault
54 are omitted; mixed usage raises ``OperationalError``.
55 """
56
57 def __init__(self, field: Field) -> None:
58 self.field = field
59
60 def __repr__(self) -> str:
61 return f"DatabaseDefault({self.field.model_field_name!r})"
62
63 def __str__(self) -> str:
64 return "<DB_DEFAULT>"
65
66 def __bool__(self) -> bool:
67 """Returns False so that ``if instance.field:`` is falsy for unset db_default fields.
68
69 This is consistent with "no value has been set yet". Users who need to
70 distinguish between DatabaseDefault and other falsy values should use
71 ``isinstance(value, DatabaseDefault)``.
72 """
73 return False
74
75
76class OnDelete(StrEnum):

Callers 4

test_reprMethod · 0.90
test_bool_is_falseMethod · 0.90
test_strMethod · 0.90
get_db_default_valueMethod · 0.85

Calls

no outgoing calls

Tested by 3

test_reprMethod · 0.72
test_bool_is_falseMethod · 0.72
test_strMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…