BaseField class¶
edgy.core.db.fields.base.BaseField
¶
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
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
¶
inject_default_on_partial_update
class-attribute
instance-attribute
¶
registry
property
¶
(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
¶
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
customize_default_for_server_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
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
operator_to_clause
¶
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
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
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
get_columns
¶
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
embed_field
¶
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
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
get_default_values
¶
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
clean
¶
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
to_model
¶
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
get_global_constraints
¶
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
get_embedded_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
get_column_names
¶
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. |