Model
class¶
edgy.Model
¶
Model(*args, **kwargs)
Bases: ModelRowMixin
, DeclarativeMixin
, DatabaseMixin
, AdminMixin
, DumpMixin
, EdgyBaseModel
The Model
class represents an Edgy ORM model, serving as the foundation for
database table mapping and interactions.
This class combines various mixins to provide a comprehensive set of functionalities,
including row-level operations (ModelRowMixin
), declarative capabilities for
SQLAlchemy model generation (DeclarativeMixin
), database connectivity
(DatabaseMixin
), administrative features (AdminMixin
), and data dumping
capabilities (DumpMixin
). It inherits from EdgyBaseModel
for core model
features and uses BaseModelMeta
as its metaclass to handle model registration
and setup.
Models defined inheriting from this class can be automatically converted into SQLAlchemy declarative models, facilitating database schema generation and ORM operations.
Example
import edgy
from edgy import Database, Registry
# Initialize a database connection and a registry for models.
database = Database("sqlite:///db.sqlite")
models = Registry(database=database)
class User(edgy.Model):
'''
The User model represents a 'users' table in the database.
If no table name is explicitly provided in the Meta class,
Edgy automatically infers it (e.g., "users" from "User").
'''
# Define model fields using Edgy's field types.
id: int = edgy.IntegerField(primary_key=True, autoincrement=True)
is_active: bool = edgy.BooleanField(default=False)
class Meta:
# Associate the model with a registry for database operations.
registry = models
Initializes the DatabaseMixin.
PARAMETER | DESCRIPTION |
---|---|
*args
|
Variable length argument list.
TYPE:
|
**kwargs
|
Arbitrary keyword arguments.
TYPE:
|
Source code in edgy/core/db/models/mixins/db.py
271 272 273 274 275 276 277 278 279 280 |
|
table
deletable
property
writable
¶
table
Returns the SQLAlchemy table associated with the model instance.
If the table is not already set on the instance, it will be built dynamically based on the active schema.
RETURNS | DESCRIPTION |
---|---|
Table
|
The SQLAlchemy |
pkcolumns
property
¶
pkcolumns
Returns the names of the primary key columns for the model instance.
If not already cached, it builds them based on the model's table.
RETURNS | DESCRIPTION |
---|---|
Sequence[str]
|
A sequence of strings representing the primary key column names. |
pknames
property
¶
pknames
Returns the logical names of the primary key fields for the model.
RETURNS | DESCRIPTION |
---|---|
Sequence[str]
|
A sequence of strings representing the primary key field names. |
identifying_db_fields
cached
property
¶
identifying_db_fields
Returns the columns used for loading the model instance. Defaults to primary key columns.
can_load
property
¶
can_load
Checks if the model instance can be loaded from the database. Requires a registry, not to be abstract, and all identifying fields to have values.
:return: True if the model can be loaded, False otherwise.
_edgy_private_attrs
class-attribute
¶
_edgy_private_attrs = PrivateAttr(default={'__show_pk__', '__using_schema__', '__no_load_trigger_attrs__', '__deletion_with_signals__', 'database', 'transaction'})
_db_loaded_or_deleted
property
¶
_db_loaded_or_deleted
Indicates if the model instance is loaded from the database or marked as deleted.
:return: True if loaded or deleted, False otherwise.
_loaded_or_deleted
property
¶
_loaded_or_deleted
Deprecated: Use _db_loaded_or_deleted
instead.
Indicates if the model instance is loaded from the database or marked as deleted.
:return: True if loaded or deleted, False otherwise.
signals
property
¶
signals
Deprecated: Use meta.signals
instead.
Returns the broadcaster for signals.
fields
property
¶
fields
Deprecated: Use meta.fields
instead.
Returns a dictionary of the model's fields.
Meta
¶
Inner Meta
class for configuring model-specific options.
ATTRIBUTE | DESCRIPTION |
---|---|
abstract |
If
TYPE:
|
registry |
If
TYPE:
|
get_columns_for_name
¶
get_columns_for_name(name)
Retrieves the SQLAlchemy columns associated with a given field name.
PARAMETER | DESCRIPTION |
---|---|
name
|
The name of the field.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Sequence[Column]
|
A sequence of SQLAlchemy |
Source code in edgy/core/db/models/mixins/db.py
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 |
|
identifying_clauses
¶
identifying_clauses(prefix='')
Generates SQLAlchemy clauses for identifying the current model instance.
These clauses are typically used in WHERE conditions for update and delete operations, based on the model's identifying database fields (usually primary keys).
PARAMETER | DESCRIPTION |
---|---|
prefix
|
An optional prefix to apply to column names in the clauses.
(Currently, this feature is not fully implemented and will
raise a
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[Any]
|
A list of SQLAlchemy clause elements. |
RAISES | DESCRIPTION |
---|---|
NotImplementedError
|
If a prefix is provided. |
Source code in edgy/core/db/models/mixins/db.py
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 |
|
load
async
¶
load(only_needed=False)
Loads the current model instance's data from the database.
This method fetches the record corresponding to the instance's identifying
clauses and updates the instance's attributes. If only_needed
is True,
it skips loading if the instance is already loaded or marked as deleted.
PARAMETER | DESCRIPTION |
---|---|
only_needed
|
If True, loads data only if the instance is not already loaded or deleted.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ObjectNotFound
|
If no row is found in the database corresponding to the instance's identifying clauses. |
Source code in edgy/core/db/models/mixins/db.py
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 |
|
update
async
¶
update(**kwargs)
Updates the current model instance in the database with the provided keyword arguments.
This method triggers pre-update and post-update signals.
PARAMETER | DESCRIPTION |
---|---|
**kwargs
|
Keyword arguments representing the fields and their new values to update.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
The updated model instance. |
Source code in edgy/core/db/models/mixins/db.py
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 |
|
real_save
async
¶
real_save(force_insert, values)
Performs the actual save operation, determining whether to insert or update.
This method checks for the existence of the instance in the database and
decides whether to perform an _insert
or _update
operation. It also
handles pre-save and post-save signals.
PARAMETER | DESCRIPTION |
---|---|
force_insert
|
If True, forces an insert operation regardless of whether the instance already exists in the database.
TYPE:
|
values
|
A dictionary of specific values to save, or a set of field names to explicitly mark as modified for a partial update. If None, all extracted database fields are considered.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
The saved model instance. |
Source code in edgy/core/db/models/mixins/db.py
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 |
|
save
async
¶
save(force_insert=False, values=None, force_save=None)
Saves the current model instance to the database.
This method acts as a public entry point for saving, encapsulating the logic for deciding between an insert or update operation. It also handles context variable management for the save process.
PARAMETER | DESCRIPTION |
---|---|
force_insert
|
If True, forces an insert operation even if the instance might already exist.
TYPE:
|
values
|
A dictionary of specific values to save, or a set of field names to explicitly mark as modified for a partial update. If None, all extracted database fields are considered.
TYPE:
|
force_save
|
Deprecated. Use
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Model
|
The saved model instance. |
Source code in edgy/core/db/models/mixins/db.py
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 |
|
raw_delete
async
¶
raw_delete(*, skip_post_delete_hooks, remove_referenced_call)
Performs the low-level delete operation from the database.
This method handles pre-delete signals, cascades deletions (if configured), and post-delete cleanup.
PARAMETER | DESCRIPTION |
---|---|
skip_post_delete_hooks
|
If True, post-delete hooks will not be executed.
TYPE:
|
remove_referenced_call
|
A boolean or string indicating if the deletion is triggered by a referenced call (e.g., cascade delete). If a string, it represents the field name that triggered it.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
The number of rows deleted. |
Source code in edgy/core/db/models/mixins/db.py
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
|
delete
async
¶
delete(skip_post_delete_hooks=False)
Deletes the current model instance from the database.
This method triggers pre-delete and post-delete signals.
PARAMETER | DESCRIPTION |
---|---|
skip_post_delete_hooks
|
If True, post-delete hooks will not be executed.
TYPE:
|
Source code in edgy/core/db/models/mixins/db.py
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 |
|
load_recursive
async
¶
load_recursive(only_needed=False, only_needed_nest=False, _seen=None)
Recursively loads related model instances.
:param only_needed: If True, only load if the instance is not already loaded. :param only_needed_nest: If True, stop recursion for nested instances if already loaded. :param _seen: A set of seen model keys to prevent infinite recursion.
Source code in edgy/core/db/models/base.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
|
model_dump
¶
model_dump(show_pk=None, **kwargs)
An updated and enhanced version of the Pydantic model_dump
method.
This method provides fine-grained control over how the model's data is serialized into a dictionary. It specifically addresses: - Enforcing the inclusion of primary key fields. - Correctly handling fields marked for exclusion. - Applying custom logic for fields that retrieve their values via getters or require special serialization (e.g., related models, composite fields).
PARAMETER | DESCRIPTION |
---|---|
self
|
The instance of the Pydantic
TYPE:
|
show_pk
|
An optional boolean flag. If
TYPE:
|
**kwargs
|
Arbitrary keyword arguments that are passed directly to the
underlying Pydantic
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
A |
dict[str, Any]
|
inclusions, exclusions, and special field handling. |
Source code in edgy/core/db/models/mixins/dump.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 116 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 163 164 165 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 197 198 199 200 201 202 |
|
build
classmethod
¶
build(schema=None, metadata=None)
Builds and returns the SQLAlchemy table representation for the model.
This method constructs the sqlalchemy.Table
object, including columns,
unique constraints, indexes, and global constraints, based on the model's
meta information and specified schema.
PARAMETER | DESCRIPTION |
---|---|
schema
|
An optional schema name to apply to the table.
TYPE:
|
metadata
|
An optional
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Table
|
The constructed SQLAlchemy |
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If the model's registry is not set. |
Source code in edgy/core/db/models/mixins/db.py
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 |
|
execute_post_save_hooks
async
¶
execute_post_save_hooks(fields, is_update)
Executes post-save hooks for relevant fields.
:param fields: A sequence of field names that were affected by the save operation. :param is_update: True if the operation was an update, False for creation.
Source code in edgy/core/db/models/base.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
execute_pre_save_hooks
async
¶
execute_pre_save_hooks(column_values, original, is_update)
Executes pre-save hooks for relevant fields.
:param column_values: Dictionary of new column values. :param original: Dictionary of original column values. :param is_update: True if the operation is an update, False for creation. :return: A dictionary of values returned by pre-save callbacks.
Source code in edgy/core/db/models/base.py
374 375 376 377 378 379 380 381 382 383 384 385 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 411 412 413 414 415 416 417 |
|
extract_column_values
classmethod
¶
extract_column_values(extracted_values, is_update=False, is_partial=False, phase='', instance=None, model_instance=None, evaluate_values=False)
Extracts and validates column values, applying transformations and defaults.
:param extracted_values: Dictionary of values to extract and validate.
:param is_update: True if the operation is an update.
:param is_partial: True if it's a partial update/creation.
:param phase: The current phase of extraction.
:param instance: The current instance being processed.
:param model_instance: The model instance context.
:param evaluate_values: If True, callable values in extracted_values
will be
evaluated.
:return: A dictionary of validated column values.
Source code in edgy/core/db/models/base.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
|
get_real_class
classmethod
¶
get_real_class()
Returns the concrete (non-proxy) class of the model instance.
If the current instance is a proxy model, it returns its parent (the original model class). Otherwise, it returns the class itself.
RETURNS | DESCRIPTION |
---|---|
type[BaseModelType]
|
type[BaseModelType]: The real, non-proxy class of the model. |
Source code in edgy/core/db/models/types.py
431 432 433 434 435 436 437 438 439 440 441 442 443 |
|
extract_db_fields
¶
extract_db_fields(only=None)
Extracts and returns a dictionary of database-related fields from the model instance.
This includes direct model fields and SQLAlchemy column attributes, but excludes related fields which are handled separately due to their disjoint nature.
PARAMETER | DESCRIPTION |
---|---|
only
|
An optional container of field names to include
in the extraction. If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
dict[str, Any]: A dictionary where keys are field names and values are their corresponding database values. |
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If |
Source code in edgy/core/db/models/types.py
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
|
get_instance_name
¶
get_instance_name()
Returns the lowercase name of the model's class.
This is typically used for generating default table names or for identification purposes.
RETURNS | DESCRIPTION |
---|---|
str
|
The lowercase name of the model's class.
TYPE:
|
Source code in edgy/core/db/models/types.py
482 483 484 485 486 487 488 489 490 491 492 |
|
create_model_key
¶
create_model_key()
Generates a unique cache key for the model instance.
The key is composed of the model's class name and the string representation of its primary key column values. This key can be used for caching model instances to improve performance.
RETURNS | DESCRIPTION |
---|---|
tuple
|
A tuple representing the unique cache key for the model instance.
TYPE:
|
Source code in edgy/core/db/models/types.py
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
|
transform_input
classmethod
¶
transform_input(kwargs, phase='', instance=None, drop_extra_kwargs=False)
Transforms input keyword arguments by applying field-specific modifications
and to_model
transformations.
:param kwargs: The input keyword arguments to transform. :param phase: The current phase of transformation. :param instance: The model instance being transformed. :param drop_extra_kwargs: If True, extra kwargs not defined in model fields will be dropped. :return: The transformed keyword arguments.
Source code in edgy/core/db/models/base.py
189 190 191 192 193 194 195 196 197 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 228 229 230 231 232 233 234 235 236 |
|
join_identifiers_to_string
¶
join_identifiers_to_string(*, sep=', ', sep_inner='=')
Joins the identifying database fields and their values into a string.
:param sep: Separator for multiple identifier-value pairs. :param sep_inner: Separator between identifier and its value. :return: A string representation of identifying fields.
Source code in edgy/core/db/models/base.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
__setattr__
¶
__setattr__(key, value)
Custom __setattr__
method to handle specific attribute assignments.
If the __using_schema__
attribute is set, it clears the cached
_table
to ensure the table is rebuilt with the new schema.
PARAMETER | DESCRIPTION |
---|---|
key
|
The name of the attribute to set.
TYPE:
|
value
|
The value to assign to the attribute.
TYPE:
|
Source code in edgy/core/db/models/mixins/db.py
668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
|
_agetattr_helper
async
¶
_agetattr_helper(name, getter)
Asynchronous helper for getattr to load the model and retrieve attributes.
:param name: The name of the attribute to retrieve. :param getter: The getter method for the attribute, if available. :return: The value of the attribute. :raises AttributeError: If the attribute is not found.
Source code in edgy/core/db/models/base.py
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 |
|
__getattribute__
¶
__getattribute__(name)
Custom getter for model attributes.
Handles retrieving Edgy's private attributes from _edgy_namespace
.
:param name: The name of the attribute to retrieve. :return: The value of the attribute. :raises AttributeError: If the attribute is not found in private namespace.
Source code in edgy/core/db/models/base.py
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
|
__getattr__
¶
__getattr__(name)
Custom getter for model attributes when not found through normal lookup.
This method handles: 1. Initialization of managers on first access. 2. Redirection of attribute access to getter fields. 3. Triggering a one-off database query to populate foreign key relationships, ensuring it runs only once per foreign key.
:param name: The name of the attribute to retrieve. :return: The value of the attribute.
Source code in edgy/core/db/models/base.py
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 |
|
__delattr__
¶
__delattr__(name)
Custom deleter for model attributes.
Handles deleting Edgy's private attributes from _edgy_namespace
.
:param name: The name of the attribute to delete. :raises AttributeError: If the attribute is not found in private namespace.
Source code in edgy/core/db/models/base.py
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
|
__eq__
¶
__eq__(other)
Compares two EdgyBaseModel instances for equality.
Equality is determined by comparing their table name, registry, and the values of their identifying database fields.
:param other: The other object to compare with. :return: True if the instances are equal, False otherwise.
Source code in edgy/core/db/models/base.py
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
|
model_dump_json
¶
model_dump_json(**kwargs)
Dumps the model data into a JSON string.
This method leverages model_dump
with mode="json"
and then uses orjson
for efficient JSON serialization, which is faster than Python's built-in json
module.
PARAMETER | DESCRIPTION |
---|---|
self
|
The instance of the
|
**kwargs
|
Arbitrary keyword arguments passed to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
A |
Source code in edgy/core/db/models/mixins/dump.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
get_admin_marshall_config
classmethod
¶
get_admin_marshall_config(*, phase, for_schema)
Generates a dictionary representing the marshall configuration for the admin interface.
This configuration dictates how model fields are handled during different administrative operations (e.g., creation, update) and schema generation.
PARAMETER | DESCRIPTION |
---|---|
cls
|
The Edgy Model class for which the marshall configuration is being generated.
TYPE:
|
phase
|
The current phase of the administrative operation, typically 'create', 'update', or 'read'. This influences field exclusions and read-only states.
TYPE:
|
for_schema
|
A boolean indicating whether the configuration is intended for schema generation (e.g., OpenAPI documentation) or for actual data marshalling.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
A dictionary containing the marshall configuration, including |
dict[str, Any]
|
specifications for fields, read-only exclusions, primary key |
dict[str, Any]
|
read-only status, and autoincrement exclusions. |
Source code in edgy/core/db/models/mixins/admin.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
get_admin_marshall_class
classmethod
¶
get_admin_marshall_class(*, phase, for_schema=False)
Generates a dynamic Marshall class specifically for the admin interface.
This allows for custom marshalling behavior based on the current administrative operation phase and whether it's for schema generation.
PARAMETER | DESCRIPTION |
---|---|
cls
|
The Edgy Model class for which the admin marshall class is being generated.
TYPE:
|
phase
|
The current phase of the administrative operation (e.g., 'create', 'update', 'read'). This phase is used to configure the underlying marshall_config.
TYPE:
|
for_schema
|
A boolean indicating whether the generated marshall class is intended for schema generation. If True, additional properties are forbidden, aligning with strict schema definitions.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
type[Marshall]
|
A dynamically created subclass of |
type[Marshall]
|
with the appropriate |
type[Marshall]
|
admin interface. |
Source code in edgy/core/db/models/mixins/admin.py
61 62 63 64 65 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 |
|
get_admin_marshall_for_save
classmethod
¶
get_admin_marshall_for_save(instance=None, /, **kwargs)
Generates a Marshall instance for saving (creating or updating) an Edgy model through the admin interface.
This method determines the appropriate phase ('create' or 'update')
based on whether an instance is provided, and then creates a
corresponding AdminMarshall
instance.
PARAMETER | DESCRIPTION |
---|---|
cls
|
The Edgy Model class for which the marshall instance is being generated.
TYPE:
|
instance
|
An optional existing model instance. If provided, the operation is considered an 'update'; otherwise, it's a 'create'.
TYPE:
|
kwargs
|
Additional keyword arguments to pass to the
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Marshall
|
An instance of |
Marshall
|
model record or updating an existing one. |
Source code in edgy/core/db/models/mixins/admin.py
103 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 135 |
|
add_to_registry
classmethod
¶
add_to_registry(registry, name='', database='keep', *, replace_related_field=False, on_conflict='error')
A public wrapper for real_add_to_registry
.
This method provides a convenient interface for registering a model class
with a given registry, forwarding all parameters to real_add_to_registry
.
PARAMETER | DESCRIPTION |
---|---|
registry
|
The
TYPE:
|
name
|
An optional name to assign to the model in the registry.
TYPE:
|
database
|
Specifies how the database connection should be handled.
TYPE:
|
replace_related_field
|
Indicates whether existing related fields should be replaced.
TYPE:
|
on_conflict
|
Defines the behavior when a model with the same name is already registered.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
type[BaseModelType]
|
The registered model class. |
Source code in edgy/core/db/models/mixins/db.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
|
get_active_instance_schema
¶
get_active_instance_schema(check_schema=True, check_tenant=True)
Retrieves the active schema for the current model instance.
This method first checks if a schema is explicitly set for the instance.
If not, it defers to get_active_class_schema
to determine the schema
based on class-level configurations and global context.
PARAMETER | DESCRIPTION |
---|---|
check_schema
|
If True, checks for a global schema in the context.
TYPE:
|
check_tenant
|
If True, considers tenant-specific schemas.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str | None
|
The active schema as a string, or None if no schema is found. |
Source code in edgy/core/db/models/mixins/db.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
|
get_active_class_schema
classmethod
¶
get_active_class_schema(check_schema=True, check_tenant=True)
Retrieves the active schema for the model class.
This method determines the schema based on the class's __using_schema__
attribute, global schema context, and the model's configured database schema.
PARAMETER | DESCRIPTION |
---|---|
check_schema
|
If True, checks for a global schema in the context.
TYPE:
|
check_tenant
|
If True, considers tenant-specific schemas when checking the global schema.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
The active schema as a string. |
Source code in edgy/core/db/models/mixins/db.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
|
copy_edgy_model
classmethod
¶
copy_edgy_model(registry=None, name='', unlink_same_registry=True, on_conflict='error', **kwargs)
Copies the model class and optionally registers it with another registry.
This method creates a deep copy of the model, including its fields and managers. It can also reconfigure foreign key and many-to-many relationships to point to models within a new registry or disable backreferences.
PARAMETER | DESCRIPTION |
---|---|
registry
|
An optional
TYPE:
|
name
|
An optional new name for the copied model. If not provided, the original model's name is used.
TYPE:
|
unlink_same_registry
|
If True, and the
TYPE:
|
on_conflict
|
Defines the behavior if a model with the same name already
exists in the target registry (if
TYPE:
|
**kwargs
|
Additional keyword arguments to pass to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
type[Self]
|
The newly created and copied model class. |
Source code in edgy/core/db/models/mixins/db.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
|
_update
async
¶
_update(is_partial, kwargs, pre_fn, post_fn, instance)
Internal method to perform an update operation on a model instance in the database.
This method handles the extraction of column values, execution of pre-save hooks, database interaction for updating records, and post-save hook execution.
PARAMETER | DESCRIPTION |
---|---|
is_partial
|
A boolean indicating if this is a partial update.
TYPE:
|
kwargs
|
A dictionary of key-value pairs representing the fields to update.
TYPE:
|
pre_fn
|
An asynchronous callable to be executed before the update.
TYPE:
|
post_fn
|
An asynchronous callable to be executed after the update.
TYPE:
|
instance
|
The model instance or queryset initiating the update.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int | None
|
The number of rows updated, or None if no update was performed. |
Source code in edgy/core/db/models/mixins/db.py
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 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 |
|
check_exist_in_db
async
¶
check_exist_in_db(only_needed=False)
Checks if the current model instance exists in the database.
PARAMETER | DESCRIPTION |
---|---|
only_needed
|
If True, performs the check only if the instance's loaded or deleted status is not conclusive.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
True if the instance exists in the database, False otherwise. |
Source code in edgy/core/db/models/mixins/db.py
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 |
|
_insert
async
¶
_insert(evaluate_values, kwargs, pre_fn, post_fn, instance)
Internal method to perform an insert operation for a model instance into the database.
This method handles the extraction of column values, execution of pre-save hooks, database insertion, and post-save hook execution.
PARAMETER | DESCRIPTION |
---|---|
evaluate_values
|
A boolean indicating whether values should be evaluated before insertion (e.g., for default values).
TYPE:
|
kwargs
|
A dictionary of key-value pairs representing the fields to insert.
TYPE:
|
pre_fn
|
An asynchronous callable to be executed before the insert.
TYPE:
|
post_fn
|
An asynchronous callable to be executed after the insert.
TYPE:
|
instance
|
The model instance or queryset initiating the insert.
TYPE:
|
Source code in edgy/core/db/models/mixins/db.py
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 |
|
add_global_field_constraints
classmethod
¶
add_global_field_constraints(schema=None, metadata=None)
Adds global constraints to an existing SQLAlchemy table.
This method is particularly useful for applying schema-specific or tenant-specific constraints to a table that has already been built.
PARAMETER | DESCRIPTION |
---|---|
schema
|
An optional schema name associated with the table.
TYPE:
|
metadata
|
An optional
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Table
|
The SQLAlchemy |
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If the model's registry is not set. |
Source code in edgy/core/db/models/mixins/db.py
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 |
|
_get_unique_constraints
classmethod
¶
_get_unique_constraints(fields)
Constructs and returns a SQLAlchemy UniqueConstraint
object.
This method handles different input types for defining unique constraints,
including a single field name, a collection of field names, or a
UniqueConstraint
object. It also generates a unique name for the constraint
if not explicitly provided.
PARAMETER | DESCRIPTION |
---|---|
fields
|
The fields (or a
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
UniqueConstraint | None
|
A SQLAlchemy |
Source code in edgy/core/db/models/mixins/db.py
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 |
|
_get_indexes
classmethod
¶
_get_indexes(index)
Constructs and returns a SQLAlchemy Index
object based on an Index
definition.
PARAMETER | DESCRIPTION |
---|---|
index
|
The
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Index | None
|
A SQLAlchemy |
Source code in edgy/core/db/models/mixins/db.py
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 |
|
not_set_transaction
¶
not_set_transaction(*, force_rollback=False, **kwargs)
Returns a database transaction for the assigned database.
This method is designed to be assigned as the transaction
property for
model instances, allowing them to initiate database transactions.
PARAMETER | DESCRIPTION |
---|---|
force_rollback
|
If True, forces the transaction to roll back.
TYPE:
|
**kwargs
|
Additional keyword arguments to pass to the database's transaction method.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Transaction
|
A |
Source code in edgy/core/db/models/mixins/db.py
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 |
|
declarative
classmethod
¶
declarative()
Returns the SQLAlchemy Declarative model representation of the Edgy model.
This is a convenience class method that acts as an entry point to obtain
the declarative version of the current Edgy model. It delegates the actual
transformation process to generate_model_declarative
.
PARAMETER | DESCRIPTION |
---|---|
cls
|
The Edgy model class (e.g.,
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Any
|
An |
Any
|
model class, which can then be used with SQLAlchemy ORM functionalities. |
Source code in edgy/core/db/models/mixins/generics.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
generate_model_declarative
classmethod
¶
generate_model_declarative()
Transforms an Edgy model's SQLAlchemy Core Table into an SQLAlchemy Declarative model.
This method dynamically creates a new class that inherits from SQLAlchemy's
Declarative Base. It assigns the Edgy model's SQLAlchemy Core Table to the
__table__
attribute of the new class, making it a declarative model.
Additionally, it identifies and configures SQLAlchemy ORM relationships
for any foreign key fields defined in the Edgy model.
PARAMETER | DESCRIPTION |
---|---|
cls
|
The Edgy model class (e.g.,
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Any
|
An |
Any
|
model class, complete with mapped relationships. |
Source code in edgy/core/db/models/mixins/generics.py
55 56 57 58 59 60 61 62 63 64 65 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 116 117 |
|
can_load_from_row
classmethod
¶
can_load_from_row(row, table)
Checks if a model class can be instantiated and populated from a given SQLAlchemy row and table.
This method verifies if the model's registry exists, if it's not an abstract model, and if all primary key columns for the model are present and not None in the provided row's mapping.
PARAMETER | DESCRIPTION |
---|---|
row
|
The SQLAlchemy row object containing the data.
TYPE:
|
table
|
The SQLAlchemy table object associated with the row.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
True if the model can be loaded from the row, False otherwise.
TYPE:
|
Source code in edgy/core/db/models/mixins/row.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
from_sqla_row
async
classmethod
¶
from_sqla_row(row, tables_and_models, select_related=None, prefetch_related=None, only_fields=None, is_defer_fields=False, exclude_secrets=False, using_schema=None, database=None, prefix='', old_select_related_value=None, reference_select=None)
Converts a SQLAlchemy Row
object into an Edgy Model
instance.
This is a class method that processes a SQLAlchemy row, populating the model's
fields, including handling select_related
and prefetch_related
relationships.
It intelligently constructs the model instance by iterating through selected
fields, managing prefixes for joined tables, and applying deferred or secret
field exclusions.
PARAMETER | DESCRIPTION |
---|---|
row
|
The SQLAlchemy row result to convert.
TYPE:
|
tables_and_models
|
A dictionary mapping prefixes to tuples of SQLAlchemy Table objects and Edgy Model types, representing the tables and models involved in the query.
TYPE:
|
select_related
|
An optional sequence of relationship names to eager-load. These relationships will be joined in the main query.
TYPE:
|
prefetch_related
|
An optional sequence of
TYPE:
|
only_fields
|
An optional sequence of field names to include in the model instance. If specified, only these fields will be populated.
TYPE:
|
is_defer_fields
|
A boolean indicating whether fields are deferred. If True, the model instance will be a proxy model with deferred field loading.
TYPE:
|
exclude_secrets
|
A boolean indicating whether secret fields should be excluded from the populated model instance.
TYPE:
|
using_schema
|
An optional schema name to use for the model.
TYPE:
|
database
|
An optional database instance to associate with the model.
TYPE:
|
prefix
|
An optional prefix used for columns in the row mapping,
typically for joined tables in
TYPE:
|
old_select_related_value
|
An optional existing model instance
to update with the new row data, used in recursive
TYPE:
|
reference_select
|
An optional dictionary specifying how to map specific columns from the row to model fields, especially for aliased columns or complex selects.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Model | None
|
Model | None: A fully populated Edgy Model instance, or None if the model |
Model | None
|
cannot be loaded from the row due to missing primary key values in joined |
Model | None
|
relationships. |
RAISES | DESCRIPTION |
---|---|
QuerySetError
|
If a field specified in |
NotImplementedError
|
If prefetching from other databases is attempted, as this feature is not yet supported. |
Source code in edgy/core/db/models/mixins/row.py
55 56 57 58 59 60 61 62 63 64 65 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 116 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 163 164 165 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 197 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 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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 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 |
|
__should_ignore_related_name
classmethod
¶
__should_ignore_related_name(related_name, select_related)
Determines whether a foreign key related name should be ignored during model
population, typically if it's already covered by a select_related
statement.
PARAMETER | DESCRIPTION |
---|---|
related_name
|
The name of the foreign key relationship.
TYPE:
|
select_related
|
A sequence of strings representing the
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
True if the related name should be ignored, False otherwise.
TYPE:
|
Source code in edgy/core/db/models/mixins/row.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
|
create_model_key_from_sqla_row
classmethod
¶
create_model_key_from_sqla_row(row, row_prefix='')
Builds a unique cache key for a model instance based on its class name and primary key values extracted from a SQLAlchemy row.
PARAMETER | DESCRIPTION |
---|---|
row
|
The SQLAlchemy row object from which to extract primary key values.
TYPE:
|
row_prefix
|
An optional prefix for column names in the row mapping, used when dealing with joined tables.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple
|
A tuple representing the unique key for the model instance.
TYPE:
|
Source code in edgy/core/db/models/mixins/row.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
|
__set_prefetch
async
classmethod
¶
__set_prefetch(row, model, row_prefix, related)
Sets a prefetched relationship on a model instance. This method handles the logic of retrieving and associating the prefetched data.
PARAMETER | DESCRIPTION |
---|---|
row
|
The SQLAlchemy row from which the main model was constructed.
TYPE:
|
model
|
The Edgy Model instance to which the prefetched data will be attached.
TYPE:
|
row_prefix
|
The prefix used for columns in the SQLAlchemy row, representing the main model's table.
TYPE:
|
related
|
The Prefetch object specifying the relationship to prefetch.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
QuerySetError
|
If creating a reverse path is not possible (e.g., for unidirectional fields). |
NotImplementedError
|
If prefetching from other databases is attempted. |
Source code in edgy/core/db/models/mixins/row.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
|
__handle_prefetch_related
async
classmethod
¶
__handle_prefetch_related(row, model, prefix, tables_and_models, prefetch_related)
Manages the execution of all prefetch_related
queries for a given model instance.
This method iterates through the specified prefetch relationships, checks for
collisions, and initiates the asynchronous loading of related data.
PARAMETER | DESCRIPTION |
---|---|
row
|
The SQLAlchemy row from which the main model was constructed.
TYPE:
|
model
|
The Edgy Model instance for which prefetch relationships are to be handled.
TYPE:
|
prefix
|
The prefix used for columns in the SQLAlchemy row, representing the main model's table.
TYPE:
|
tables_and_models
|
A dictionary mapping prefixes to tuples of SQLAlchemy Table objects and Edgy Model types, representing the tables and models involved in the query.
TYPE:
|
prefetch_related
|
A sequence of
TYPE:
|
RAISES | DESCRIPTION |
---|---|
QuerySetError
|
If a conflicting attribute is found that would be overwritten by a prefetch operation. |
Source code in edgy/core/db/models/mixins/row.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 |
|
real_add_to_registry
classmethod
¶
real_add_to_registry(**kwargs)
Adds the model to its associated registry, handling admin model registration.
This method is called during the model's initialization process to register
it with the Registry
instance specified in its Meta
class. It also
conditionally adds the model to the admin's list of registered models
if in_admin
is not False
and a registry is present.
PARAMETER | DESCRIPTION |
---|---|
**kwargs
|
Arbitrary keyword arguments passed to the superclass method.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
type[Model]
|
type[Model]: The model class after being added to the registry. |
Source code in edgy/core/db/models/model.py
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 |
|
generate_proxy_model
classmethod
¶
generate_proxy_model()
Generates a lightweight proxy model based on the current model.
A proxy model is a shallow copy of the original model. It is not added to
the registry and typically has its field references replaced to be generic
(e.g., edgy.fields.BigIntegerField
becomes int
). This is useful for
internal operations or when a stripped-down version of the model is needed
without affecting the main registry.
RETURNS | DESCRIPTION |
---|---|
type[Model]
|
type[Model]: A new model class representing the proxy model. |
Source code in edgy/core/db/models/model.py
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|