use existing encode_json to avoid code duplication, rename queryset customization param and move it to Meta, move docs to models from inheritance

This commit is contained in:
collerek
2022-01-26 17:59:00 +01:00
parent 33b492216d
commit 0e167dc89f
14 changed files with 74 additions and 112 deletions

View File

@ -78,22 +78,21 @@ class ItemConfig(ormar.Model):
pairs: pydantic.Json = ormar.JSON(default=["2", "3"])
class QuerySetCls(QuerySet):
async def first_or_404(self, *args, **kwargs):
entity = await self.get_or_none(*args, **kwargs)
if not entity:
# maybe HTTPException in fastapi
raise ValueError("customer not found")
return entity
class Customer(ormar.Model):
class Meta:
metadata = metadata
database = database
tablename = "customer"
class QuerySetCls(QuerySet):
async def first_or_404(self, *args, **kwargs):
entity = await self.get_or_none(*args, **kwargs)
if not entity:
# maybe HTTPException in fastapi
raise ValueError("customer not found")
return entity
__queryset_cls__ = QuerySetCls
queryset_class = QuerySetCls
id: Optional[int] = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=32)

View File

@ -1,19 +0,0 @@
import orjson
import json
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)
expected_bytes = expected_str.encode()
assert isinstance(expected_bytes, bytes)
assert isinstance(to_str(expected_bytes), str)
assert "1" == to_str(1)