A CRUD library for Tortoise ORM
Project description
CRUD Tortoise ORM Extension
Project Overview
This project is an extension that implements CRUD operations based on Tortoise ORM, providing a simple and efficient way to manage database records.
Installation Instructions
Install the wh-fastapi-crud-tortoise package:
pip install wh-fastapi-crud-tortoise
Usage
Here is a basic example of how to use the CRUD extension:
from fastapi import FastAPI
from pydantic import BaseModel, ConfigDict
from datetime import datetime, date
from typing import Optional, Union
from datetime import datetime
from tortoise import fields
from src import CrudModel, TortoiseCRUDRouter, resp_success, resp_fail
from tortoise.contrib.fastapi import register_tortoise
class DummyModel(CrudModel):
"""Record Table"""
name = fields.CharField(max_length=255, null=False)
age = fields.IntField()
salary = fields.FloatField()
is_active = fields.BooleanField(default=True)
birthdate = fields.DateField()
created_at = fields.DatetimeField()
notes = fields.TextField()
json_data = fields.JSONField(null=True, default=None)
class Meta:
table = "dummy"
ID_TYPE = str
class DummyCreateDTO(BaseModel):
name: str
age: int
salary: float
is_active: bool
birthdate: date
created_at: datetime
notes: str
json_data: Optional[object] = None
class DummyDTO(BaseModel):
id: Optional[ID_TYPE] = None
name: Optional[str] = None
age: Optional[int] = None
salary: Optional[float] = None
is_active: Optional[bool] = None
birthdate: Optional[date] = None
created_at: Optional[datetime] = None
notes: Optional[str] = None
json_data: Optional[object] = None
model_config = ConfigDict(from_attributes=True)
dummy_router = TortoiseCRUDRouter(
schema=DummyDTO, create_schema=DummyCreateDTO, db_model=DummyModel, prefix="dummy"
)
@dummy_router.post("/custom_router")
async def test(para1: int, para2: str):
return resp_success(data="test custom router")
app = FastAPI(title="FastapiCrudPro")
register_tortoise(
app,
db_url="sqlite://tortoise.sqlite3",
modules={"models": ["demo"]},
generate_schemas=True,
add_exception_handlers=True,
)
app.include_router(dummy_router)
@app.get("/")
def read_root():
return {"Hello": "World"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app="demo:app", host="127.0.0.1", port=8010, reload=True)
API Documentation
To view the API documentation, open your browser and navigate to http://127.0.0.1:8010/docs.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wh_fastapi_crud_tortoise-0.1.30-py3-none-any.whl.
File metadata
- Download URL: wh_fastapi_crud_tortoise-0.1.30-py3-none-any.whl
- Upload date:
- Size: 48.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70b8bb347df6d70c8dde7bda1ce225be9b58a5ea030bb1b73fca36ad1742847d
|
|
| MD5 |
4aef5271a252c6ae682e6cef3049ca88
|
|
| BLAKE2b-256 |
69519f4eb8dc80607060e56a5a8662a4479ee51ecc9ba16eaef8c6148228eb26
|