allow in fastapi to return related models with only pk populated

This commit is contained in:
collerek
2020-11-09 15:40:26 +01:00
parent 9eca23ef74
commit 3b3f0445f4
3 changed files with 35 additions and 3 deletions

View File

@ -30,7 +30,7 @@ class UndefinedType: # pragma no cover
Undefined = UndefinedType() Undefined = UndefinedType()
__version__ = "0.4.1" __version__ = "0.4.2"
__all__ = [ __all__ = [
"Integer", "Integer",
"BigInteger", "BigInteger",

View File

@ -1,6 +1,7 @@
from typing import Any, List, Optional, TYPE_CHECKING, Type, Union from typing import Any, List, Optional, TYPE_CHECKING, Type, Union
import sqlalchemy import sqlalchemy
from pydantic import BaseModel, create_model
from sqlalchemy import UniqueConstraint from sqlalchemy import UniqueConstraint
import ormar # noqa I101 import ormar # noqa I101
@ -9,6 +10,7 @@ from ormar.fields.base import BaseField
if TYPE_CHECKING: # pragma no cover if TYPE_CHECKING: # pragma no cover
from ormar.models import Model, NewBaseModel from ormar.models import Model, NewBaseModel
from ormar.fields import ManyToManyField
def create_dummy_instance(fk: Type["Model"], pk: Any = None) -> "Model": def create_dummy_instance(fk: Type["Model"], pk: Any = None) -> "Model":
@ -23,6 +25,15 @@ def create_dummy_instance(fk: Type["Model"], pk: Any = None) -> "Model":
return fk(**init_dict) return fk(**init_dict)
def create_dummy_model(
base_model: Type["Model"],
pk_field: Type[Union[BaseField, "ForeignKeyField", "ManyToManyField"]],
) -> Type["BaseModel"]:
fields = {f"{pk_field.name}": (pk_field.__type__, None)}
dummy_model = create_model(f"PkOnly{base_model.get_name(lower=False)}", **fields) # type: ignore
return dummy_model
class UniqueColumns(UniqueConstraint): class UniqueColumns(UniqueConstraint):
pass pass
@ -40,10 +51,11 @@ def ForeignKey( # noqa CFQ002
) -> Any: ) -> Any:
fk_string = to.Meta.tablename + "." + to.get_column_alias(to.Meta.pkname) fk_string = to.Meta.tablename + "." + to.get_column_alias(to.Meta.pkname)
to_field = to.Meta.model_fields[to.Meta.pkname] to_field = to.Meta.model_fields[to.Meta.pkname]
pk_only_model = create_dummy_model(to, to_field)
__type__ = ( __type__ = (
Union[to_field.__type__, to] Union[to_field.__type__, to, pk_only_model]
if not nullable if not nullable
else Optional[Union[to_field.__type__, to]] else Optional[Union[to_field.__type__, to, pk_only_model]]
) )
namespace = dict( namespace = dict(
__type__=__type__, __type__=__type__,

View File

@ -64,6 +64,12 @@ async def get_items():
return items return items
@app.get("/items/raw/", response_model=List[Item])
async def get_raw_items():
items = await Item.objects.all()
return items
@app.post("/items/", response_model=Item) @app.post("/items/", response_model=Item)
async def create_item(item: Item): async def create_item(item: Item):
await item.save() await item.save()
@ -76,6 +82,12 @@ async def create_category(category: Category):
return category return category
@app.get("/items/{item_id}")
async def get_item(item_id: int):
item = await Item.objects.get(pk=item_id)
return item
@app.put("/items/{item_id}") @app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item): async def update_item(item_id: int, item: Item):
item_db = await Item.objects.get(pk=item_id) item_db = await Item.objects.get(pk=item_id)
@ -113,6 +125,14 @@ def test_all_endpoints():
items = [Item(**item) for item in response.json()] items = [Item(**item) for item in response.json()]
assert items[0].name == "New name" assert items[0].name == "New name"
response = client.get("/items/raw/")
items = [Item(**item) for item in response.json()]
assert items[0].name == "New name"
response = client.get(f"/items/{item.pk}")
new_item = Item(**response.json())
assert new_item == item
response = client.delete(f"/items/{item.pk}") response = client.delete(f"/items/{item.pk}")
assert response.json().get("deleted_rows", "__UNDEFINED__") != "__UNDEFINED__" assert response.json().get("deleted_rows", "__UNDEFINED__") != "__UNDEFINED__"
response = client.get("/items/") response = client.get("/items/")