Package¶
models
¶
viur-models — SQLModel-backed models for ViUR.
Model/Record (base classes), Field (bone metadata), the bone-typed
field types, structure_for_model, and the boot wiring install/setup.
Importing has no side effects. Bone mapping tables: docs/bones.md.
VIUR_META_KEY
module-attribute
¶
Key of the bone metadata in FieldInfo.json_schema_extra.
BONE_TYPE_REGISTRY
module-attribute
¶
BONE_TYPE_REGISTRY: dict[type, BoneType] = {}
Python type → BoneType, matched along the MRO; an Annotated marker wins.
Code
module-attribute
¶
Code = t.Annotated[str, BoneType('raw.code', replace=True, extras={'indexed': False})]
CodeBone ("raw.code"), not indexed.
Color
module-attribute
¶
Color = t.Annotated[str, BoneType('color', replace=True, extras=dict(_COLOR_EXTRAS))]
ColorBone ("color", mode: rgb).
Country
module-attribute
¶
SelectCountryBone ("select.country"), validated by pydantic-extra-types.
Credential
module-attribute
¶
Credential = t.Annotated[str, BoneType('str.credential', extras={'maxlength': None, 'minlength': None}, write_only=True)]
CredentialBone ("str.credential"), write-only.
Json
module-attribute
¶
Json = t.Annotated[dict, BoneType('raw.json', replace=True, extras={'schema': {}, 'indexed': False})]
JsonBone ("raw.json"); needs an explicit sa_type.
Password
module-attribute
¶
Password = t.Annotated[str, BoneType('password', replace=True, emptyvalue='', write_only=True, extras={'maxlength': 254, 'minlength': None, 'tests': PASSWORD_TESTS, 'test_threshold': 4})]
PasswordBone ("password"), write-only. Hashing is NOT automatic — do it in onAdd/onEdit.
Phone
module-attribute
¶
Phone = t.Annotated[str, BoneType('str.phone', extras=dict(_PHONE_EXTRAS))]
PhoneBone ("str.phone"); max_length=15 for parity. Real validation: pydantic PhoneNumber.
SortIndex
module-attribute
¶
SortIndex = t.Annotated[float, BoneType('numeric.sortindex', extras={'clone_behavior': {'strategy': 'set_default'}})]
SortIndexBone ("numeric.sortindex").
Text
module-attribute
¶
Text = t.Annotated[str, BoneType('text', extras={'valid_html': None}, replace=True, emptyvalue='')]
TextBone ("text").
Uid
module-attribute
¶
Uid = t.Annotated[str, BoneType('uid', replace=True, extras={'fillchar': '*', 'length': 13, 'pattern': '*', 'readonly': True, 'unique': 1, 'clone_behavior': {'strategy': 'set_default'}, 'compute': {'method': 'Once'}})]
UidBone ("uid"): readonly, unique. The module supplies the value.
Uri
module-attribute
¶
Uri = t.Annotated[str, BoneType('uri', replace=True, extras=dict(_URI_EXTRAS))]
UriBone ("uri"), default hints. Real validation: pydantic AnyUrl with sa_type=String.
Model
¶
Bases: SQLModel
Base for SQL-backed models. The structure is built at class definition (unmappable types fail fast) and cached per class.
Source code in src/viur/models/base.py
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 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 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 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 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 | |
errors
property
writable
¶
Client-input errors (SkeletonInstance.errors counterpart). Kept in __dict__:
loaded rows skip __init__, so pydantic private attrs may not exist.
viur_structure
classmethod
¶
Skeleton-compatible structure dict, fresh deep copy (the caller owns it).
viur_crossstore
classmethod
¶
name → SkeletonRefMarker, cached.
viur_relations
classmethod
¶
Relations per relations_for_model, cached.
Source code in src/viur/models/base.py
viur_write_only
classmethod
¶
Write-only field names, cached.
viur_dump
¶
SkeletonInstance.dump()-shaped values: opaque key, ISO datetimes, enum values,
relations in RelationalBone shape. bones restricts the output; without it, a
client bonelist attached by SQLList (_viur_bones) does.
Source code in src/viur/models/base.py
viur_from_client
classmethod
¶
skel.fromClient() counterpart. Unknown and read-only fields are dropped. Returns
(instance, []) or (form, errors) — form is unvalidated (_viur_form),
never persist it.
Source code in src/viur/models/base.py
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 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 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 | |
viur_encode_key
classmethod
¶
Primary key → opaque key string.
viur_parse_key
classmethod
¶
Primary key from a viur_key; None for malformed or foreign keys.
Source code in src/viur/models/base.py
Record
¶
Bases: SQLModel
Base for nested record values (RelSkel analogue): no table, no key, no system fields.
Source code in src/viur/models/base.py
ModelsConfig
¶
conf.models.* namespace owned by viur-models.
Source code in src/viur/models/config.py
CrossStoreIndex
¶
Bases: SQLModel
Reverse index target key → (table, row, field) of JSON-column references; maintained by
SQLList on write. SkeletonLink tables index themselves via key.
Source code in src/viur/models/crossstore.py
SkeletonLink
¶
Bases: SQLModel
Base for link-table-backed multiple cross-store references (one row per target).
Carries key (datastore key, part of the PK) and the dest snapshot; subclasses add
the parent FK and set viur_kind (required) plus the viur_link_* ClassVars. The
parent relationship needs cascade="all, delete-orphan".
Source code in src/viur/models/crossstore.py
RecordJSON
¶
Bases: TypeDecorator
JSON column for nested records: record_cls instances (or lists) dump on write, validate on read.
Source code in src/viur/models/db.py
RelationLink
¶
BoneType
dataclass
¶
Annotation marker setting a field's bone type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Structure |
required |
extras
|
dict | None
|
Additional structure keys. |
None
|
replace
|
bool
|
Drop the Python type's structure; |
False
|
emptyvalue
|
Any
|
Emitted |
None
|
write_only
|
bool
|
Dumps emit the |
False
|
Source code in src/viur/models/types.py
Language
¶
Language[X]: {lang: value} dict in a JSON column; languages from
Field(languages=…) or set_default_languages.
Source code in src/viur/models/types.py
install
¶
install(*, engine: str | None = None, sqlite_file: str | None = None, postgres_dsn: str | None = None, bigquery_dsn: str | None = None, engine_options: dict | None = None, databases: dict[str, dict] | None = None, refresh_hooks: bool = True, **refresh_hook_kwargs: Any) -> Engine
Attach conf.models, apply the settings, build the engines, install the
refresh hooks. Once, before core.setup(). None leaves a setting untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
str | None
|
Preset of the default database: |
None
|
engine_options
|
dict | None
|
Extra |
None
|
databases
|
dict[str, dict] | None
|
Entries for |
None
|
refresh_hooks
|
bool
|
|
True
|
refresh_hook_kwargs
|
Any
|
Passed to |
{}
|
Source code in src/viur/models/boot.py
setup
¶
setup(engine: Engine | None = None, *, migrations: str | PathLike | None = None, initial_revision: bool = True, **scaffold_kwargs: Any) -> str | None
Report the schema revision of every configured database; after core.setup().
Memory preset: create_all of that database's tables. migrations generates the
missing Alembic scaffold (dev server only) and, if it generated any, the first revision.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
Engine | None
|
Engine to inspect instead of the configured ones. |
None
|
migrations
|
str | PathLike | None
|
Directory for |
None
|
initial_revision
|
bool
|
|
True
|
scaffold_kwargs
|
Any
|
Passed to |
{}
|
Returns:
| Type | Description |
|---|---|
str | None
|
Stamped revision of the default database, or |
Source code in src/viur/models/boot.py
map_validation_error
¶
missing → NotSet, anything else → Invalid; fieldPath is pydantic's loc.
Source code in src/viur/models/client.py
install_config
¶
install_config() -> ModelsConfig
Attach ModelsConfig to conf.models; idempotent.
Source code in src/viur/models/config.py
FileRef
¶
FileRef(ref_keys: Sequence[str] = ('name', 'mimetype', 'size', 'width', 'height', 'dlkey', 'serving_url', 'derived', 'public'), **kwargs: Any) -> Any
FileBone analogue (relational.tree.leaf.file.file); valid_mime_types/public via extras.
Source code in src/viur/models/crossstore.py
SkeletonRef
¶
SkeletonRef(kind: str, ref_keys: Sequence[str] = ('name',), *, module: str | None = None, type_suffix: str | None = None, multiple: bool = False, format: str | None = None, extras: dict | None = None) -> Any
Cross-store reference type for skeleton kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref_keys
|
Sequence[str]
|
Target bones in |
('name',)
|
module
|
str | None
|
Serving module (default: |
None
|
type_suffix
|
str | None
|
|
None
|
format
|
str | None
|
Display format default; |
None
|
extras
|
dict | None
|
Extra structure keys, emitted verbatim. |
None
|
Source code in src/viur/models/crossstore.py
UserRef
¶
UserBone analogue (relational.user).
Source code in src/viur/models/crossstore.py
install_refresh_hooks
¶
Wrap Skeleton.postSavedHandler/postDeletedHandler to defer refresh_for_target
for referenced kinds. Once at boot; idempotent. Skeletons overriding the handlers without
super() must call it from their own onEdited/onDeleted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
missing_on_delete
|
str
|
|
'set_null'
|
countdown
|
int
|
Task delay in seconds. |
10
|
Source code in src/viur/models/crossstore.py
refresh_crossstore
¶
Re-read every referenced target and rewrite stale snapshots (full scan per model;
SkeletonLink tables via key). missing="set_null" clears vanished targets.
Returns {"checked", "refreshed", "cleared"}.
Source code in src/viur/models/crossstore.py
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 | |
refresh_for_target
¶
Update every snapshot referencing key: JSON columns via the index, SkeletonLink rows
via key. missing as in refresh_crossstore. Called by the refresh hooks.
Source code in src/viur/models/crossstore.py
Field
¶
Field(default: Any = PydanticUndefined, *, descr: str | None = None, required: bool | None = None, visible: bool = True, readonly: bool = False, params: dict | None = None, values: dict | None = None, compute: dict | None = None, languages: Sequence[str] | None = None, format: str | None = None, tags: str | Sequence[str] | None = None, schema_extra: dict | None = None, **kwargs: Any) -> Any
sqlmodel.Field() plus bone metadata. Constraints pydantic/SQL express
(max_length, ge/le, nullability, defaults) are derived, not repeated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
descr
|
str | None
|
Display name (default: title-cased field name). |
None
|
required
|
bool | None
|
Override of the pydantic derivation. |
None
|
visible
|
bool
|
Shown in client UIs. |
True
|
readonly
|
bool
|
Read-only bone; forces |
False
|
params
|
dict | None
|
|
None
|
values
|
dict | None
|
|
None
|
compute
|
dict | None
|
|
None
|
languages
|
Sequence[str] | None
|
Language codes of a |
None
|
format
|
str | None
|
Display format of record/relational bones, emitted verbatim. |
None
|
tags
|
str | Sequence[str] | None
|
Classification tags ( |
None
|
schema_extra
|
dict | None
|
Extra |
None
|
kwargs
|
Any
|
Passed to |
{}
|
Source code in src/viur/models/fields.py
structure_for_model
¶
structure_for_model(cls: type, *, include_relations: bool = True, resolve_refs: bool = True) -> dict
Skeleton-compatible structure dict. Primary key → key bone; an FK consumed by a
to-one relation → one relational.<kind> bone under the relation's name.
include_relations=False: scalar-only (relskel); resolve_refs=False: no
datastore lookup for cross-store relskel.
Source code in src/viur/models/structure.py
Spatial
¶
SpatialBone ("spatial") type factory; values are (lat, lng), stored as JSON.