orjson test and doc

This commit is contained in:
huangsong
2022-01-21 10:03:00 +08:00
parent d55a29c040
commit 6297846819
2 changed files with 39 additions and 1 deletions

View File

@ -570,4 +570,38 @@ class Category(CreateDateFieldsModel, AuditCreateModel):
code: int = ormar.Integer() code: int = ormar.Integer()
``` ```
That way you can inherit from both create and update classes if needed, and only one of them otherwise. 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")
```

View File

@ -1,3 +1,4 @@
import orjson
import json import json
from ormar.queryset.utils import to_str from ormar.queryset.utils import to_str
@ -5,6 +6,9 @@ from ormar.queryset.utils import to_str
def test_to_str(): def test_to_str():
expected_str = "[]" expected_str = "[]"
val = orjson.dumps([])
assert expected_str == to_str(val)
val = json.dumps([]) val = json.dumps([])
assert expected_str == to_str(val) assert expected_str == to_str(val)