()
| 343 | |
| 344 | |
| 345 | async def raw_data(): |
| 346 | # extract raw data in a form of dicts or tuples |
| 347 | # note that this skips the validation(!) as models are |
| 348 | # not created from parsed data |
| 349 | |
| 350 | # get list of objects as dicts |
| 351 | assert await Book.objects.values() == [ |
| 352 | {"id": 1, "author": 1, "title": "The Hobbit", "year": 1937}, |
| 353 | {"id": 2, "author": 1, "title": "The Lord of the Rings", "year": 1955}, |
| 354 | {"id": 4, "author": 2, "title": "The Witcher", "year": 1990}, |
| 355 | {"id": 5, "author": 1, "title": "The Silmarillion", "year": 1977}, |
| 356 | ] |
| 357 | |
| 358 | # get list of objects as tuples |
| 359 | assert await Book.objects.values_list() == [ |
| 360 | (1, 1, "The Hobbit", 1937), |
| 361 | (2, 1, "The Lord of the Rings", 1955), |
| 362 | (4, 2, "The Witcher", 1990), |
| 363 | (5, 1, "The Silmarillion", 1977), |
| 364 | ] |
| 365 | |
| 366 | # filter data - note how you always get a list |
| 367 | assert await Book.objects.filter(title="The Hobbit").values() == [ |
| 368 | {"id": 1, "author": 1, "title": "The Hobbit", "year": 1937} |
| 369 | ] |
| 370 | |
| 371 | # select only wanted fields |
| 372 | assert await Book.objects.filter(title="The Hobbit").values(["id", "title"]) == [ |
| 373 | {"id": 1, "title": "The Hobbit"} |
| 374 | ] |
| 375 | |
| 376 | # if you select only one column you could flatten it with values_list |
| 377 | assert await Book.objects.values_list("title", flatten=True) == [ |
| 378 | "The Hobbit", |
| 379 | "The Lord of the Rings", |
| 380 | "The Witcher", |
| 381 | "The Silmarillion", |
| 382 | ] |
| 383 | |
| 384 | # to read more about extracting raw values |
| 385 | # visit: https://collerek.github.io/ormar/queries/aggregations/ |
| 386 | |
| 387 | |
| 388 | async def with_connect(function): |
nothing calls this directly
no test coverage detected