BaseField
class¶
edgy.core.db.fields.base.BaseField
¶
BaseField(*, default=Undefined, server_default=Undefined, **kwargs)
Bases: BaseFieldType
, FieldInfo
The foundational class for all Edgy model fields.
This class extends BaseFieldType
and pydantic.fields.FieldInfo
to provide
core functionalities for defining model attributes, including database column
mapping, validation, and default value handling. It also offers mechanisms
for dynamic operator mapping for query building.
ATTRIBUTE | DESCRIPTION |
---|---|
owner |
The model class to which this field belongs.
TYPE:
|
operator_mapping |
A dictionary mapping common operator strings to
SQLAlchemy's
TYPE:
|
auto_compute_server_default |
Controls the automatic computation of server
defaults. Can be
TYPE:
|
Initializes a new BaseField
instance.
This constructor processes default values, server defaults, and other
field configurations, integrating them with Pydantic's FieldInfo
.
PARAMETER | DESCRIPTION |
---|---|
default
|
The default Python value for the field. If
TYPE:
|
server_default
|
The default value to be set by the database server.
If
TYPE:
|
**kwargs
|
Additional keyword arguments to be passed to
TYPE:
|
Source code in edgy/core/db/fields/base.py
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
operator_mapping
class-attribute
instance-attribute
¶
operator_mapping = {'is': 'is_', 'in': 'in_', 'exact': '__eq__', 'not': '__ne__', 'gt': '__gt__', 'ge': '__ge__', 'gte': '__ge__', 'lt': '__lt__', 'lte': '__le__', 'le': '__le__'}
auto_compute_server_default
class-attribute
instance-attribute
¶
auto_compute_server_default = False
inject_default_on_partial_update
class-attribute
instance-attribute
¶
inject_default_on_partial_update = False
registry
property
¶
registry
(Deprecated) Provides access to the Edgy Registry
associated with the field's owner model.
This property is deprecated; use self.owner.meta.registry
instead.
get_server_default
¶
get_server_default()
Retrieves the server-side default value for the column.
This method determines the appropriate server default, considering
explicitly defined server_default
, the field's default
value,
and the auto_compute_server_default
setting.
RETURNS | DESCRIPTION |
---|---|
Any
|
The server default value, which can be |
Any
|
or a SQLAlchemy text construct. |
Source code in edgy/core/db/fields/base.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
customize_default_for_server_default
¶
customize_default_for_server_default(default)
Customizes the default value for use as a server-side default.
This method ensures that callable defaults are executed and that
the resulting value is wrapped in a sqlalchemy.text
construct
for appropriate server-side default handling.
PARAMETER | DESCRIPTION |
---|---|
default
|
The original default Python value, which might be a callable.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Any
|
A |
Any
|
server-side default. |
Source code in edgy/core/db/fields/base.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
get_columns_nullable
¶
get_columns_nullable()
Determines if the database column(s) corresponding to this field should be nullable.
This method considers the field's explicit null
setting and global
FORCE_FIELDS_NULLABLE
context variable.
RETURNS | DESCRIPTION |
---|---|
bool
|
|
Source code in edgy/core/db/fields/base.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
operator_to_clause
¶
operator_to_clause(field_name, operator, table, value)
Converts a given operator and value into a SQLAlchemy clause for filtering.
This method maps common operation codes to SQLAlchemy's ColumnElement
methods,
allowing for dynamic construction of database queries. It handles special cases
like case-insensitive exact matches ('iexact') and null checks ('isnull'),
as well as various string containment operations.
PARAMETER | DESCRIPTION |
---|---|
field_name
|
The name of the column to apply the operator to.
TYPE:
|
operator
|
The operation code (e.g., 'iexact', 'contains', 'isnull').
TYPE:
|
table
|
The SQLAlchemy Table object the column belongs to.
TYPE:
|
value
|
The value to compare against.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Any
|
A SQLAlchemy clause suitable for use in a query's WHERE statement. |
RAISES | DESCRIPTION |
---|---|
KeyError
|
If 'field_name' does not correspond to an existing column in the table. |
AttributeError
|
If the mapped operator does not exist as a method on the column. |
Source code in edgy/core/db/fields/base.py
204 205 206 207 208 209 210 211 212 213 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 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 |
|
is_required
¶
is_required()
Checks if the field is required for model instantiation and database inserts.
A field is considered not required if it's a primary key with autoincrement, is nullable, has a server default, or has a Python default.
RETURNS | DESCRIPTION |
---|---|
bool
|
|
Source code in edgy/core/db/fields/base.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
has_default
¶
has_default()
Checks if the field has a default value set in Python.
This includes cases where default
is None
but explicitly set.
RETURNS | DESCRIPTION |
---|---|
bool
|
|
Source code in edgy/core/db/fields/base.py
292 293 294 295 296 297 298 299 300 301 302 303 304 |
|
get_columns
¶
get_columns(name)
Returns a sequence of SQLAlchemy Column objects associated with this field.
This is a base implementation that returns an empty list, as BaseField
itself doesn't directly correspond to columns. Subclasses like Field
will override this to return actual columns.
PARAMETER | DESCRIPTION |
---|---|
name
|
The name of the field.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Sequence[Column]
|
An empty sequence of SQLAlchemy Column objects. |
Source code in edgy/core/db/fields/base.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
|
embed_field
¶
embed_field(prefix, new_fieldname, owner=None, parent=None)
Embeds this field into a new context, potentially modifying its name and owner.
This method creates a copy of the field and updates its name
and owner
attributes. If a parent
field specifies prefix_column_name
, it
updates the column_name
of the copied field accordingly. Returns None
if embedding is not supported for this field type.
PARAMETER | DESCRIPTION |
---|---|
prefix
|
The prefix to be added to the field's new name.
TYPE:
|
new_fieldname
|
The new full name of the field after embedding.
TYPE:
|
owner
|
The new owner model class for the embedded field.
TYPE:
|
parent
|
The parent field if this field is being embedded within another composite field.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
BaseField | None
|
A copy of the field with updated |
BaseField | None
|
embedding is not permitted. |
Source code in edgy/core/db/fields/base.py
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
|
get_default_value
¶
get_default_value()
Retrieves the default Python value for a single-valued field.
If the default is a callable, it is executed to get the value.
RETURNS | DESCRIPTION |
---|---|
Any
|
The default value of the field, or |
Source code in edgy/core/db/fields/base.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|
get_default_values
¶
get_default_values(field_name, cleaned_data)
Retrieves default values for the field, to be applied during data cleaning.
For single-column fields, if the field name is not already present in
cleaned_data
, its default value is returned. Multi-value fields
should override this method.
PARAMETER | DESCRIPTION |
---|---|
field_name
|
The name of the field.
TYPE:
|
cleaned_data
|
The dictionary of already cleaned data.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
A dictionary containing the default value for the field if it's not |
dict[str, Any]
|
already in |
Source code in edgy/core/db/fields/base.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
|
clean
¶
clean(field_name, value, for_query=False)
Validates and transforms a Python value into a format suitable for database operations.
This method is used when preparing data for insertion or update into the database, or when building query predicates. Multi-column fields should return a dictionary mapping column names to their cleaned values.
PARAMETER | DESCRIPTION |
---|---|
field_name
|
The logical name of the field.
TYPE:
|
value
|
The Python value of the field.
TYPE:
|
for_query
|
If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
dict[str, Any]: A dictionary where keys are database column names and values are their cleaned representations. |
Source code in edgy/core/db/fields/types.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
to_model
¶
to_model(field_name, value)
Converts database column values back into the Python representation for the Edgy model.
This is the inverse of clean
. It takes raw values retrieved from the database
and transforms them into the expected Python type for the model instance.
PARAMETER | DESCRIPTION |
---|---|
field_name
|
The logical name of the field.
TYPE:
|
value
|
The raw value(s) retrieved from the database.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
dict[str, Any]: A dictionary where keys are field names and values are their Python representations. |
Source code in edgy/core/db/fields/types.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
get_global_constraints
¶
get_global_constraints(name, columns, schemes=())
Returns global SQLAlchemy constraints or indexes that apply across multiple columns or at the table level (e.g., unique constraints involving multiple fields).
PARAMETER | DESCRIPTION |
---|---|
name
|
The logical name of the field.
TYPE:
|
columns
|
The SQLAlchemy Column objects associated with this field.
TYPE:
|
schemes
|
Database schema names (if applicable).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Sequence[Constraint | Index]
|
Sequence[sqlalchemy.Constraint | sqlalchemy.Index]: A sequence of SQLAlchemy constraints or indexes. |
Source code in edgy/core/db/fields/types.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
get_embedded_fields
¶
get_embedded_fields(field_name, fields)
Defines and returns additional fields that should be "embedded" or added to the model's schema dynamically.
This is useful for fields that generate related or sub-fields. The returned
fields should be new instances or copies, and their owner
should be set.
PARAMETER | DESCRIPTION |
---|---|
field_name
|
The logical name of the field.
TYPE:
|
fields
|
The existing fields defined on the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict[str, BaseFieldType]
|
dict[str, BaseFieldType]: A dictionary of new field names to |
Source code in edgy/core/db/fields/types.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
get_column_names
¶
get_column_names(name='')
Retrieves the set of database column names associated with this field.
PARAMETER | DESCRIPTION |
---|---|
name
|
The logical name of the field. If empty, uses
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
frozenset[str]
|
frozenset[str]: A frozenset of database column names. |
Source code in edgy/core/db/fields/types.py
322 323 324 325 326 327 328 329 330 331 332 333 334 |
|