RefForeignKey class¶
edgy.fields.RefForeignKey
¶
Bases: ForeignKeyFieldFactory, list
A specialized foreign key field designed to work with ModelRef classes.
This field allows a model to establish a relationship with a ModelRef,
which represents a reference to another model without necessarily defining
a direct foreign key column in the database. It is particularly useful
for handling generic relationships or when the related model's details
are managed through a separate "through" model or a ModelRef.
It enforces that the to argument is an instance of ModelRef and that
the ModelRef subclass has a __related_name__ defined.
| ATTRIBUTE | DESCRIPTION |
|---|---|
field_type |
The Python type representing the field (always
TYPE:
|
field_bases |
The base classes for this field, including
TYPE:
|
methods_overwritable_by_factory
class-attribute
instance-attribute
¶
build_field
classmethod
¶
Constructs and returns a new field instance based on the factory's configuration.
This method orchestrates the creation of a BaseFieldType instance.
It determines the column type, Pydantic type, and constraints from the
provided kwargs and the factory's properties. It then instantiates
the field and calls overwrite_methods to apply any factory-defined
method overrides.
| PARAMETER | DESCRIPTION |
|---|---|
**kwargs
|
Keyword arguments for configuring the field.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BaseFieldType
|
The newly constructed and configured field instance.
TYPE:
|
Source code in edgy/core/db/fields/factories.py
overwrite_methods
classmethod
¶
Overwrites methods on the given field_obj with methods defined in the factory.
This method iterates through the factory's own methods. If a method's name
is present in methods_overwritable_by_factory, it replaces the corresponding
method on the field_obj. It handles class methods and ensures proper
staticmethod wrapping for consistent behavior across Python versions.
| PARAMETER | DESCRIPTION |
|---|---|
field_obj
|
The field instance whose methods are to be overwritten.
TYPE:
|
Source code in edgy/core/db/fields/factories.py
repack
classmethod
¶
Repacks methods on the given field_obj that were previously overwritten
by the factory.
This method is used to re-apply the partial and staticmethod wrappers
to methods that were already overwritten. This can be necessary in scenarios
where field objects are serialized/deserialized or otherwise lose their
original method bindings. It ensures that the context (cls, field_obj)
remains correctly bound.
| PARAMETER | DESCRIPTION |
|---|---|
field_obj
|
The field instance whose methods are to be repacked.
TYPE:
|
Source code in edgy/core/db/fields/factories.py
validate
classmethod
¶
Validates the parameters for a foreign key field.
This method enforces rules specific to foreign keys, such as:
- on_delete must not be null.
- If SET_NULL is used for on_delete or on_update, the field must be nullable.
- related_name must be a string if provided and not False.
It also sets a default null value if not provided.
| PARAMETER | DESCRIPTION |
|---|---|
kwargs
|
A dictionary of parameters for the foreign key field.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
FieldDefinitionError
|
If any foreign key validation rule is violated. |
Source code in edgy/core/db/fields/factories.py
get_constraints
classmethod
¶
Returns a sequence of constraints applicable to the column.
This method can be overridden by subclasses to provide specific database constraints for the column associated with this field type.
| PARAMETER | DESCRIPTION |
|---|---|
kwargs
|
Keyword arguments provided during field creation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Sequence[Any]
|
Sequence[Any]: A sequence of constraint objects. |
Source code in edgy/core/db/fields/factories.py
get_column_type
classmethod
¶
Returns the SQL column type for the field.
For regular fields, this will return the appropriate SQLAlchemy column type.
For meta fields (fields that don't directly map to a database column),
it should return None.
| PARAMETER | DESCRIPTION |
|---|---|
kwargs
|
Keyword arguments provided during field creation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
The SQLAlchemy column type or
TYPE:
|
Source code in edgy/core/db/fields/factories.py
get_pydantic_type
classmethod
¶
Returns the Pydantic type for the field.
This type is used by Pydantic for validation and serialization. By default,
it returns the field_type attribute of the factory.
| PARAMETER | DESCRIPTION |
|---|---|
kwargs
|
Keyword arguments provided during field creation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
The Pydantic type associated with the field.
TYPE:
|
Source code in edgy/core/db/fields/factories.py
_get_field_cls
cached
staticmethod
¶
Internal static method to dynamically create and cache the actual field class.
This method uses lru_cache to ensure that the field class is created
only once for each FieldFactory type. It constructs a new type based
on the FieldFactory's __name__ and field_bases.
| PARAMETER | DESCRIPTION |
|---|---|
cls
|
The
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BaseFieldType
|
The dynamically created and cached field class.
TYPE:
|
Source code in edgy/core/db/fields/factories.py
modify_input
classmethod
¶
Modifies the input kwargs during model initialization.
If the field name is not present in kwargs and the phase is init_db,
it initializes the field with an empty list. This ensures that the field
is always a list, ready to store ModelRef instances.
Source code in edgy/core/db/fields/ref_foreign_key.py
embed_field
¶
Placeholder for embedding logic. RefForeignKey typically does not
embed directly into queries in the same way as traditional foreign keys,
as its values are often handled through the associated __related_name__.
Source code in edgy/core/db/fields/ref_foreign_key.py
post_save_callback
async
classmethod
¶
Asynchronous post-save callback for RefForeignKey.
This callback is executed after a model instance containing this field is saved.
It processes the list of ModelRef instances provided in value and
establishes the actual relationships by adding them to the related field
(determined by the __related_name__ on the ModelRef).
| PARAMETER | DESCRIPTION |
|---|---|
field_obj
|
The field object itself.
TYPE:
|
value
|
The list of
TYPE:
|
is_update
|
TYPE:
|
original_fn
|
The original callback function (if any).
TYPE:
|