ForeignKey
class¶
edgy.fields.ForeignKey
¶
Bases: ForeignKeyFieldFactory
, cast(Any, object)
A factory for creating ForeignKey
fields in Edgy models.
This factory extends ForeignKeyFieldFactory
to provide a convenient
interface for defining foreign key relationships. It ensures that specific
server-side default and update operations are not mistakenly applied to
foreign key fields, and validates the embed_parent
configuration.
methods_overwritable_by_factory
class-attribute
instance-attribute
¶
methods_overwritable_by_factory = frozenset(default_methods_overwritable_by_factory)
build_field
classmethod
¶
build_field(**kwargs)
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
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 163 164 |
|
overwrite_methods
classmethod
¶
overwrite_methods(field_obj)
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
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
repack
classmethod
¶
repack(field_obj)
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
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
get_constraints
classmethod
¶
get_constraints(kwargs)
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
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
get_column_type
classmethod
¶
get_column_type(kwargs)
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
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
get_pydantic_type
classmethod
¶
get_pydantic_type(kwargs)
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
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
_get_field_cls
cached
staticmethod
¶
_get_field_cls()
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
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
__new__
¶
__new__(to, **kwargs)
Creates a new ForeignKey
field instance.
This method primarily passes the to
argument (target model) and other
kwargs
to the parent ForeignKeyFieldFactory
for field construction.
PARAMETER | DESCRIPTION |
---|---|
to
|
The target model class or its string name to which this foreign key points.
TYPE:
|
**kwargs
|
Additional keyword arguments for the foreign key field.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
BaseFieldType
|
The constructed
TYPE:
|
Source code in edgy/core/db/fields/foreign_keys.py
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 |
|
validate
classmethod
¶
validate(kwargs)
Validates the parameters for a ForeignKey
field.
This method overrides the parent validate
method to enforce rules
specific to foreign keys. It explicitly disallows auto_compute_server_default
,
server_default
, and server_onupdate
to prevent issues with relational
integrity. It also validates the format of the embed_parent
argument.
PARAMETER | DESCRIPTION |
---|---|
kwargs
|
The dictionary of keyword arguments passed during field construction.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
FieldDefinitionError
|
If any validation rule is violated. |
Source code in edgy/core/db/fields/foreign_keys.py
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 |
|