Model
class¶
edgy.Model
¶
Model(*args, __show_pk__=False, __phase__='init', **kwargs)
Bases: ModelRowMixin
, DeclarativeMixin
, EdgyBaseModel
Representation of an Edgy Model
.
This also means it can generate declarative SQLAlchemy models
from anywhere by calling the Model.declarative()
function.
Example
import edgyBaseFieldType
from edgy import Database, Registry
database = Database("sqlite:///db.sqlite")
models = Registry(database=database)
class User(edgy.Model):
'''
The User model to be created in the database as a table
If no name is provided the in Meta class, it will generate
a "users" table for you.
'''
id: int = edgy.IntegerField(primary_key=True)
is_active: bool = edgy.BooleanField(default=False)
class Meta:
registry = models
PARAMETER | DESCRIPTION |
---|---|
*args |
TYPE:
|
__show_pk__ |
TYPE:
|
__phase__ |
TYPE:
|
**kwargs |
TYPE:
|
Source code in edgy/core/db/models/base.py
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 |
|
identifying_db_fields
cached
property
¶
identifying_db_fields
The columns used for loading, can be set per instance defaults to pknames
get_columns_for_name
¶
get_columns_for_name(name)
PARAMETER | DESCRIPTION |
---|---|
name |
TYPE:
|
Source code in edgy/core/db/models/base.py
290 291 292 293 294 295 296 297 298 |
|
identifying_clauses
¶
identifying_clauses()
Source code in edgy/core/db/models/base.py
300 301 302 303 304 305 306 307 308 309 310 311 |
|
load_recursive
async
¶
load_recursive(only_needed=False, only_needed_nest=False, _seen=None)
PARAMETER | DESCRIPTION |
---|---|
only_needed |
TYPE:
|
only_needed_nest |
TYPE:
|
_seen |
TYPE:
|
Source code in edgy/core/db/models/base.py
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 |
|
model_dump
¶
model_dump(show_pk=None, **kwargs)
An updated version of the model dump. It can show the pk always and handles the exclude attribute on fields correctly and contains the custom logic for fields with getters
PARAMETER | DESCRIPTION |
---|---|
show_pk |
TYPE:
|
**kwargs |
TYPE:
|
Extra Args
show_pk: bool - Enforces showing the primary key in the model_dump.
Source code in edgy/core/db/models/base.py
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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 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 |
|
build
classmethod
¶
build(schema=None, metadata=None)
Builds the SQLAlchemy table representation from the loaded fields.
PARAMETER | DESCRIPTION |
---|---|
schema |
TYPE:
|
metadata |
TYPE:
|
Source code in edgy/core/db/models/base.py
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 |
|
execute_post_save_hooks
async
¶
execute_post_save_hooks(fields, force_insert)
PARAMETER | DESCRIPTION |
---|---|
fields |
TYPE:
|
force_insert |
TYPE:
|
Source code in edgy/core/db/models/base.py
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
|
execute_pre_save_hooks
async
¶
execute_pre_save_hooks(column_values, original, force_insert)
PARAMETER | DESCRIPTION |
---|---|
column_values |
TYPE:
|
original |
TYPE:
|
force_insert |
TYPE:
|
Source code in edgy/core/db/models/base.py
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 |
|
extract_column_values
classmethod
¶
extract_column_values(extracted_values, is_update=False, is_partial=False, phase='', instance=None, model_instance=None)
PARAMETER | DESCRIPTION |
---|---|
extracted_values |
TYPE:
|
is_update |
TYPE:
|
is_partial |
TYPE:
|
phase |
TYPE:
|
instance |
TYPE:
|
model_instance |
TYPE:
|
Source code in edgy/core/db/models/base.py
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 |
|
extract_db_fields
¶
extract_db_fields(only=None)
Extracts all the db fields, model references and fields. Related fields are not included because they are disjoint.
PARAMETER | DESCRIPTION |
---|---|
only |
TYPE:
|
Source code in edgy/core/db/models/types.py
183 184 185 186 187 188 189 190 191 192 193 194 |
|
get_instance_name
¶
get_instance_name()
Returns the name of the class in lowercase.
Source code in edgy/core/db/models/types.py
196 197 198 199 200 |
|
create_model_key
¶
create_model_key()
Build a cache key for the model.
Source code in edgy/core/db/models/types.py
202 203 204 205 206 207 208 209 210 |
|
transform_input
classmethod
¶
transform_input(kwargs, phase='', instance=None)
Expand to_models and apply input modifications.
PARAMETER | DESCRIPTION |
---|---|
kwargs |
TYPE:
|
phase |
TYPE:
|
instance |
TYPE:
|
Source code in edgy/core/db/models/base.py
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 |
|
setup_model_from_kwargs
¶
setup_model_from_kwargs(kwargs)
Loops and setup the kwargs of the model
PARAMETER | DESCRIPTION |
---|---|
kwargs |
TYPE:
|
Source code in edgy/core/db/models/base.py
140 141 142 143 144 145 |
|
copy_edgy_model
classmethod
¶
copy_edgy_model(registry=None, name='', **kwargs)
Copy the model class and optionally add it to another registry.
PARAMETER | DESCRIPTION |
---|---|
registry |
TYPE:
|
name |
TYPE:
|
**kwargs |
TYPE:
|
Source code in edgy/core/db/models/base.py
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 |
|
_get_unique_constraints
classmethod
¶
_get_unique_constraints(columns)
Returns the unique constraints for the model.
The columns must be a a list, tuple of strings or a UniqueConstraint object.
:return: Model UniqueConstraint.
PARAMETER | DESCRIPTION |
---|---|
columns |
TYPE:
|
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 |
|
_get_indexes
classmethod
¶
_get_indexes(index)
Creates the index based on the Index fields
PARAMETER | DESCRIPTION |
---|---|
index |
TYPE:
|
Source code in edgy/core/db/models/base.py
470 471 472 473 474 475 |
|
__setattr__
¶
__setattr__(key, value)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
value |
TYPE:
|
Source code in edgy/core/db/models/base.py
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
|
_agetattr_helper
async
¶
_agetattr_helper(name, getter)
PARAMETER | DESCRIPTION |
---|---|
name |
TYPE:
|
getter |
TYPE:
|
Source code in edgy/core/db/models/base.py
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
|
__getattr__
¶
__getattr__(name)
Does following things 1. Initialize managers on access 2. Redirects get accesses to getter fields 3. Run an one off query to populate any foreign key making sure it runs only once per foreign key avoiding multiple database calls.
PARAMETER | DESCRIPTION |
---|---|
name |
TYPE:
|
Source code in edgy/core/db/models/base.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
|
__eq__
¶
__eq__(other)
PARAMETER | DESCRIPTION |
---|---|
other |
TYPE:
|
Source code in edgy/core/db/models/base.py
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 |
|
declarative
classmethod
¶
declarative()
Source code in edgy/core/db/models/mixins/generics.py
12 13 14 |
|
generate_model_declarative
classmethod
¶
generate_model_declarative()
Transforms a core Edgy table into a Declarative model table.
Source code in edgy/core/db/models/mixins/generics.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
from_sqla_row
async
classmethod
¶
from_sqla_row(row, select_related=None, prefetch_related=None, is_only_fields=False, only_fields=None, is_defer_fields=False, exclude_secrets=False, using_schema=None, database=None, table=None)
Class method to convert a SQLAlchemy Row result into a EdgyModel row type.
Looping through select_related fields if the query comes from a select_related operation. Validates if exists the select_related and related_field inside the models.
When select_related and related_field exist for the same field being validated, the related field is ignored as it won't override the value already collected from the select_related.
If there is no select_related, then goes through the related field where it should only return the instance of the the ForeignKey with the ID, making it lazy loaded.
:return: Model class.
PARAMETER | DESCRIPTION |
---|---|
row |
TYPE:
|
select_related |
TYPE:
|
prefetch_related |
TYPE:
|
is_only_fields |
TYPE:
|
only_fields |
TYPE:
|
is_defer_fields |
TYPE:
|
exclude_secrets |
TYPE:
|
using_schema |
TYPE:
|
database |
TYPE:
|
table |
TYPE:
|
Source code in edgy/core/db/models/mixins/row.py
23 24 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 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 |
|
__should_ignore_related_name
classmethod
¶
__should_ignore_related_name(related_name, select_related)
Validates if it should populate the related field if select related is not considered.
PARAMETER | DESCRIPTION |
---|---|
related_name |
TYPE:
|
select_related |
TYPE:
|
Source code in edgy/core/db/models/mixins/row.py
165 166 167 168 169 170 171 172 173 174 175 176 |
|
create_model_key_from_sqla_row
classmethod
¶
create_model_key_from_sqla_row(row)
Build a cache key for the model.
PARAMETER | DESCRIPTION |
---|---|
row |
TYPE:
|
Source code in edgy/core/db/models/mixins/row.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
__set_prefetch
async
classmethod
¶
__set_prefetch(row, model, related)
PARAMETER | DESCRIPTION |
---|---|
row |
TYPE:
|
model |
TYPE:
|
related |
TYPE:
|
Source code in edgy/core/db/models/mixins/row.py
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 |
|
__handle_prefetch_related
async
classmethod
¶
__handle_prefetch_related(row, model, prefetch_related)
Handles any prefetch related scenario from the model. Loads in advance all the models needed for a specific record
Recursively checks for the related field and validates if there is any conflicting
attribute. If there is, a QuerySetError
is raised.
PARAMETER | DESCRIPTION |
---|---|
row |
TYPE:
|
model |
TYPE:
|
prefetch_related |
TYPE:
|
Source code in edgy/core/db/models/mixins/row.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
generate_proxy_model
classmethod
¶
generate_proxy_model()
Generates a proxy model for each model. This proxy model is a simple shallow copy of the original model being generated.
Source code in edgy/core/db/models/model.py
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 |
|
_update
async
¶
_update(**kwargs)
Update operation of the database fields.
PARAMETER | DESCRIPTION |
---|---|
**kwargs |
TYPE:
|
Source code in edgy/core/db/models/model.py
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 |
|
update
async
¶
update(**kwargs)
PARAMETER | DESCRIPTION |
---|---|
**kwargs |
TYPE:
|
Source code in edgy/core/db/models/model.py
138 139 140 141 142 143 144 |
|
delete
async
¶
delete(skip_post_delete_hooks=False, remove_referenced_call=False)
Delete operation from the database
PARAMETER | DESCRIPTION |
---|---|
skip_post_delete_hooks |
TYPE:
|
remove_referenced_call |
TYPE:
|
Source code in edgy/core/db/models/model.py
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 |
|
load
async
¶
load(only_needed=False)
PARAMETER | DESCRIPTION |
---|---|
only_needed |
TYPE:
|
Source code in edgy/core/db/models/model.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
_insert
async
¶
_insert(**kwargs)
Performs the save instruction.
PARAMETER | DESCRIPTION |
---|---|
**kwargs |
TYPE:
|
Source code in edgy/core/db/models/model.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 |
|
save
async
¶
save(force_insert=False, values=None, force_save=None)
Performs a save of a given model instance. When creating a user it will make sure it can update existing or create a new one.
PARAMETER | DESCRIPTION |
---|---|
force_insert |
TYPE:
|
values |
TYPE:
|
force_save |
TYPE:
|
Source code in edgy/core/db/models/model.py
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 |
|
transaction
¶
transaction(*, force_rollback=False, **kwargs)
Return database transaction for the assigned database
PARAMETER | DESCRIPTION |
---|---|
force_rollback |
TYPE:
|
**kwargs |
TYPE:
|
Source code in edgy/core/db/models/model.py
314 315 316 |
|