Skip to main content

A CRUD library for Tortoise ORM

Project description

Project Introduction

This project is a CRUD extension based on Tortoise ORM.

Installation Steps

  1. Install wh-fastapi-crud-tortoise:
    pip install wh-fastapi-crud-tortoise
    
  2. Usage
    </code></pre>
    </li>
    </ol>
    <p>from fastapi import FastAPI
    from pydantic import BaseModel, ConfigDict
    from datetime import datetime, date
    from typing import Optional, Union</p>
    <p>from datetime import datetime
    from tortoise import fields
    from src import CrudModel, TortoiseCRUDRouter, resp_success, resp_fail
    from tortoise.contrib.fastapi import register_tortoise</p>
    <p>class DummyModel(CrudModel):
    """Record Table"""</p>
    <pre><code>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)
    
    
    3. Open your browser and visit `http://127.0.0.1:8010/docs` to view the application.
    
    # Contribution
    We welcome issues and pull requests to contribute to this project.
    
    # License
    This project is licensed under the MIT License.
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wh_fastapi_crud_tortoise-0.1.2-py3-none-any.whl (24.3 kB view details)

Uploaded Python 3

File details

Details for the file wh_fastapi_crud_tortoise-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for wh_fastapi_crud_tortoise-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4e0e88ff1b73ab477d07db2e4c4f52232dd4f60105f3a26ec3827d0e2f82a0a9
MD5 f6d08b7141a6083034765bb0c535974e
BLAKE2b-256 7595e442fec08164150d7ab426222facb59c58ee221fd02eac0c17ac4cb2298d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page