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
| 42 | |
| 43 | |
| 44 | class 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 | |
| 76 | class OnDelete(StrEnum): |
no outgoing calls
searching dependent graphs…