Closed avico78 closed 3 years ago
Hi @avico78. So the CRUDRouter currently does not support inserting nested objects. This is something I am looking at potentially implementing in the future.
Currently you have the option of overloading routes to provide custom functionality (docs). You could do something as below to get around your issue.
customer_router = SQLDBCrudRouter(...)
@custom_router.post("")
def custom_create_router(customer: CustomerModel)
#my custom db logic
...
app.include_router(customer_router)
Thanks @awtkns , Support of nested object would be awesome and actually sound like a real challenge , not sure if this can help solving the nested objects , but it maybe it may interest you : https://github.com/brokenloop/jsontopydantic it building a pydantic classes from a Json - even a real nested one. The core of this project coming from datamodel-code-generator (v 0.5.39 )
import json
from pydantic import Json
from typing import Dict, Any
from genson import SchemaBuilder
from datamodel_code_generator.parser.jsonschema import JsonSchemaParser
from datamodel_code_generator.model.pydantic import (
BaseModel,
CustomRootType,
DataModelField,
)
def translate(
input_text: Dict,
) -> str:
builder = SchemaBuilder()
builder.add_object(input_text)
schema = json.dumps(builder.to_schema())
parser = JsonSchemaParser(
BaseModel,
CustomRootType,
DataModelField,
text=schema,
)
return parser.parse()
if __name__=="__main__":
data2 = {"nested.":{"a":{"b":{"c":{"dList":[{"e":{"f":"val"}}]}}}}}
result = translate(data2)
print(result)
Thanks I will keep that in mind. Ok if I close this for now?
Thanks I will keep that in mind. Ok if I close this for now?
sure,i will follow :) hopefully a solution will be find for this , Thanks @awtkns ,great initiative
fastapi 0.63.0 fastapi-crudrouter 0.6.1
Trying to use SQLAlchemyCRUDRouter for joined tables , All endpoints created automatically (amazing:)).
Tried using the "create one " for input:
End up with error:
Code:
Routes:
schemas.py
models.py