Field¶
All Saffier model fields inherit from Field.
The base class is where Saffier joins together three different concerns:
- validation through an internal validator object
- SQLAlchemy column generation
- ORM-side normalization such as composite-field expansion or relation cleanup
Common options shared by most fields¶
The exact supported arguments vary by field type, but the base field behavior is where options such as these are enforced:
primary_keynulldefaultserver_defaultindexuniquecolumn_nameexcludesecret
Why the base field matters¶
When you define a custom field or debug an unexpected write behavior, the base field API is the contract to understand:
clean()converts a logical field value into one or more database columnsmodify_input()can expand or rewrite incoming payloadsget_global_constraints()can add foreign keys or indexes to the tablepre_save_callback()can generate write-time values before persistence
saffier.core.db.fields.base.Field
¶
Field(
*,
primary_key=False,
index=False,
unique=False,
**kwargs,
)
Base class for Saffier model fields.
A field coordinates three layers of behavior: runtime validation through an internal validator, SQLAlchemy column generation, and ORM-specific input or relation normalization. Concrete subclasses usually override at least the validator or the column type, while relation-aware fields also override input normalization and pre-save behavior.
Source code in saffier/core/db/fields/base.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
inject_default_on_partial_update
instance-attribute
¶
inject_default_on_partial_update = get(
"inject_default_on_partial_update", False
)
get_column
¶
get_column(name)
Build the SQLAlchemy column used to persist this field.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Logical field name declared on the model.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Column
|
sqlalchemy.Column: SQLAlchemy column configured from the field |
Column
|
options and validator metadata. |
Source code in saffier/core/db/fields/base.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
get_validator
¶
get_validator(**kwargs)
Source code in saffier/core/db/fields/base.py
127 128 | |
get_column_type
¶
get_column_type()
Source code in saffier/core/db/fields/base.py
130 131 | |
get_constraints
¶
get_constraints()
Source code in saffier/core/db/fields/base.py
133 134 | |
get_columns
¶
get_columns(name)
Source code in saffier/core/db/fields/base.py
136 137 | |
get_global_constraints
¶
get_global_constraints(name, columns, *, schema=None)
Source code in saffier/core/db/fields/base.py
139 140 141 142 143 144 145 146 147 | |
has_column
¶
has_column()
Source code in saffier/core/db/fields/base.py
149 150 | |
get_embedded_fields
¶
get_embedded_fields(field_name, existing_fields)
Source code in saffier/core/db/fields/base.py
152 153 154 155 156 157 158 | |
expand_relationship
¶
expand_relationship(value)
Source code in saffier/core/db/fields/base.py
160 161 | |
clean
¶
clean(name, value, *, for_query=False)
Normalize one logical field value into column-value pairs.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Logical field name.
TYPE:
|
value
|
User-facing field value.
TYPE:
|
for_query
|
Reserved for field implementations that need distinct query-time normalization.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
dict[str, Any]: Database payload keyed by column name. |
Source code in saffier/core/db/fields/base.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
modify_input
¶
modify_input(name, kwargs)
Source code in saffier/core/db/fields/base.py
182 183 | |
raise_for_non_default
¶
raise_for_non_default(default, server_default)
Source code in saffier/core/db/fields/base.py
185 186 | |
get_default_value
¶
get_default_value()
Source code in saffier/core/db/fields/base.py
188 189 | |
has_default
¶
has_default()
Source code in saffier/core/db/fields/base.py
191 192 | |
get_default_values
¶
get_default_values(field_name, cleaned_data)
Source code in saffier/core/db/fields/base.py
194 195 196 197 198 199 200 201 | |
is_required
¶
is_required()
Source code in saffier/core/db/fields/base.py
203 204 205 206 | |
get_is_null_clause
¶
get_is_null_clause(column)
Source code in saffier/core/db/fields/base.py
208 209 | |
get_is_empty_clause
¶
get_is_empty_clause(column)
Source code in saffier/core/db/fields/base.py
211 212 | |
operator_to_clause
¶
operator_to_clause(field_name, operator, table, value)
Translate one lookup operator into a SQLAlchemy clause.
| PARAMETER | DESCRIPTION |
|---|---|
field_name
|
Logical field name being filtered.
TYPE:
|
operator
|
Saffier lookup suffix such as
TYPE:
|
table
|
SQLAlchemy table containing the target column.
TYPE:
|
value
|
Lookup value supplied by the caller.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
SQLAlchemy boolean clause implementing the lookup.
TYPE:
|
Source code in saffier/core/db/fields/base.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |