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
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 94 95 96 97 98 99 100 101 102 | |
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
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
get_validator
¶
get_validator(**kwargs)
Source code in saffier/core/db/fields/base.py
136 137 | |
get_column_type
¶
get_column_type()
Source code in saffier/core/db/fields/base.py
139 140 | |
get_constraints
¶
get_constraints()
Source code in saffier/core/db/fields/base.py
142 143 | |
get_columns_nullable
¶
get_columns_nullable()
Resolve SQLAlchemy nullability for this field during metadata build.
The model declaration remains the canonical field definition, but migration generation can temporarily ask Saffier to render selected columns as nullable. That temporary override makes it possible to add a required field to a table that already has rows, then backfill defaults in the generated online migration before the final model constraint is enforced by later migrations.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
TYPE:
|
bool
|
override should make generated columns nullable. |
Source code in saffier/core/db/fields/base.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
get_columns
¶
get_columns(name)
Source code in saffier/core/db/fields/base.py
166 167 | |
get_global_constraints
¶
get_global_constraints(name, columns, *, schema=None)
Source code in saffier/core/db/fields/base.py
169 170 171 172 173 174 175 176 177 | |
has_column
¶
has_column()
Source code in saffier/core/db/fields/base.py
179 180 | |
get_embedded_fields
¶
get_embedded_fields(field_name, existing_fields)
Source code in saffier/core/db/fields/base.py
182 183 184 185 186 187 188 | |
expand_relationship
¶
expand_relationship(value)
Source code in saffier/core/db/fields/base.py
190 191 | |
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
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
modify_input
¶
modify_input(name, kwargs)
Source code in saffier/core/db/fields/base.py
212 213 | |
raise_for_non_default
¶
raise_for_non_default(default, server_default)
Source code in saffier/core/db/fields/base.py
215 216 | |
get_default_value
¶
get_default_value()
Source code in saffier/core/db/fields/base.py
218 219 | |
has_default
¶
has_default()
Source code in saffier/core/db/fields/base.py
221 222 | |
get_default_values
¶
get_default_values(field_name, cleaned_data)
Source code in saffier/core/db/fields/base.py
224 225 226 227 228 229 230 231 | |
is_required
¶
is_required()
Source code in saffier/core/db/fields/base.py
233 234 235 236 | |
get_is_null_clause
¶
get_is_null_clause(column)
Source code in saffier/core/db/fields/base.py
238 239 | |
get_is_empty_clause
¶
get_is_empty_clause(column)
Source code in saffier/core/db/fields/base.py
241 242 | |
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
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |