From 6297846819a4623dc9d6bcc8850a2b6f1f9a50e6 Mon Sep 17 00:00:00 2001 From: huangsong Date: Fri, 21 Jan 2022 10:03:00 +0800 Subject: [PATCH] orjson test and doc --- docs/models/inheritance.md | 36 +++++++++++++++++++++++++++++++- tests/test_queries/test_utils.py | 4 ++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/docs/models/inheritance.md b/docs/models/inheritance.md index 5282247..0cc1c9f 100644 --- a/docs/models/inheritance.md +++ b/docs/models/inheritance.md @@ -570,4 +570,38 @@ class Category(CreateDateFieldsModel, AuditCreateModel): code: int = ormar.Integer() ``` -That way you can inherit from both create and update classes if needed, and only one of them otherwise. \ No newline at end of file +That way you can inherit from both create and update classes if needed, and only one of them otherwise. + +## __queryset_cls__ + +You can define your own queryset_class(extends the `Queryset`) in your model class, default is `QuerySet` + +```python +import ormar +from ormar.queryset.queryset import QuerySet +from fastapi import HTTPException + + +class MyQuerySetClass(QuerySet): + + async def first_or_404(self, *args, **kwargs): + entity = await self.get_or_none(*args, **kwargs) + if entity is None: + # in fastapi or starlette + raise HTTPException(404) + +class Book(ormar.Model): + class Meta: + metadata = metadata + database = database + tablename = "book" + + __queryset_cls__ = MyQuerySetClass + + id: int = ormar.Integer(primary_key=True) + name: str = ormar.String(max_length=32) + + # when book not found, raise `404` in your view. + book = await Book.objects.first_or_404(name="123") + +``` \ No newline at end of file diff --git a/tests/test_queries/test_utils.py b/tests/test_queries/test_utils.py index 8f2fac4..2ed2dee 100644 --- a/tests/test_queries/test_utils.py +++ b/tests/test_queries/test_utils.py @@ -1,3 +1,4 @@ +import orjson import json from ormar.queryset.utils import to_str @@ -5,6 +6,9 @@ from ormar.queryset.utils import to_str def test_to_str(): expected_str = "[]" + val = orjson.dumps([]) + assert expected_str == to_str(val) + val = json.dumps([]) assert expected_str == to_str(val)