Model
class¶
edgy.Model
¶
Model(*args, **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 edgy
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:
|
**kwargs |
TYPE:
|
Source code in edgy/core/db/models/base.py
64 65 66 67 68 69 70 71 72 |
|
identifying_db_fields
cached
property
¶
identifying_db_fields
The columns used for loading, can be set per instance defaults to pknames
_return_load_coro_on_attr_access
class-attribute
instance-attribute
¶
_return_load_coro_on_attr_access = False
get_columns_for_name
¶
get_columns_for_name(name)
PARAMETER | DESCRIPTION |
---|---|
name |
TYPE:
|
Source code in edgy/core/db/models/base.py
209 210 211 212 213 214 215 216 217 |
|
identifying_clauses
¶
identifying_clauses()
Source code in edgy/core/db/models/base.py
219 220 221 222 223 224 225 226 |
|
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/base.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
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
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 |
|
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
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 |
|
build
classmethod
¶
build(schema=None)
Builds the SQLAlchemy table representation from the loaded fields.
PARAMETER | DESCRIPTION |
---|---|
schema |
TYPE:
|
Source code in edgy/core/db/models/base.py
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 |
|
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
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
get_instance_name
¶
get_instance_name()
Returns the name of the class in lowercase.
Source code in edgy/core/db/models/types.py
153 154 155 156 157 |
|
create_model_key
¶
create_model_key()
Build a cache key for the model.
Source code in edgy/core/db/models/types.py
159 160 161 162 163 164 165 166 167 |
|
extract_model_references
¶
extract_model_references(extracted_values, model_class=None)
Exracts any possible model references from the EdgyModel and returns a dictionary.
PARAMETER | DESCRIPTION |
---|---|
extracted_values |
TYPE:
|
model_class |
TYPE:
|
Source code in edgy/core/db/models/mixins/model_parser.py
9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
extract_column_values
¶
extract_column_values(extracted_values, model_class=None, is_update=False, is_partial=False)
Extracts all the default values from the given fields and returns the raw value corresponding to each field.
Extract the model references.
PARAMETER | DESCRIPTION |
---|---|
extracted_values |
TYPE:
|
model_class |
TYPE:
|
is_update |
TYPE:
|
is_partial |
TYPE:
|
Source code in edgy/core/db/models/mixins/model_parser.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 |
|
transform_input
classmethod
¶
transform_input(kwargs, phase)
Expand to_model.
PARAMETER | DESCRIPTION |
---|---|
kwargs |
TYPE:
|
phase |
TYPE:
|
Source code in edgy/core/db/models/base.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
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
95 96 97 98 99 100 101 102 103 104 |
|
_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
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
|
_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
397 398 399 400 401 402 |
|
__setattr__
¶
__setattr__(key, value)
PARAMETER | DESCRIPTION |
---|---|
key |
TYPE:
|
value |
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 |
|
_agetattr_helper
async
¶
_agetattr_helper(name)
PARAMETER | DESCRIPTION |
---|---|
name |
TYPE:
|
Source code in edgy/core/db/models/base.py
420 421 422 |
|
__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
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 |
|
__eq__
¶
__eq__(other)
PARAMETER | DESCRIPTION |
---|---|
other |
TYPE:
|
Source code in edgy/core/db/models/base.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
|
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)
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:
|
Source code in edgy/core/db/models/mixins/row.py
20 21 22 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 |
|
__apply_schema
classmethod
¶
__apply_schema(model, schema=None)
PARAMETER | DESCRIPTION |
---|---|
model |
TYPE:
|
schema |
TYPE:
|
Source code in edgy/core/db/models/mixins/row.py
148 149 150 151 152 153 154 |
|
__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
156 157 158 159 160 161 162 163 164 165 166 167 |
|
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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
__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
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 |
|
__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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
|
update
async
¶
update(**kwargs)
Update operation of the database fields.
PARAMETER | DESCRIPTION |
---|---|
**kwargs |
TYPE:
|
Source code in edgy/core/db/models/model.py
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 |
|
delete
async
¶
delete()
Delete operation from the database
Source code in edgy/core/db/models/model.py
72 73 74 75 76 77 78 79 80 81 |
|
load
async
¶
load(only_needed=False)
PARAMETER | DESCRIPTION |
---|---|
only_needed |
TYPE:
|
Source code in edgy/core/db/models/model.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
_save
async
¶
_save(**kwargs)
Performs the save instruction.
PARAMETER | DESCRIPTION |
---|---|
**kwargs |
TYPE:
|
Source code in edgy/core/db/models/model.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
save_model_references
async
¶
save_model_references(model_references, model_ref=None)
If there is any ModelRef declared in the model, it will generate the subsquent model reference records for that same model created.
PARAMETER | DESCRIPTION |
---|---|
model_references |
TYPE:
|
model_ref |
TYPE:
|
Source code in edgy/core/db/models/model.py
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 |
|
save
async
¶
save(force_save=False, values=None, **kwargs)
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_save |
TYPE:
|
values |
TYPE:
|
**kwargs |
TYPE:
|
Source code in edgy/core/db/models/model.py
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 |
|