Sanic Example¶
This is an example of the Tortoise-ORM Sanic integration
Usage:
python3 main.py
models.py¶
from tortoise import Model, fields
class Users(Model):
id = fields.IntField(primary_key=True)
name = fields.CharField(50)
def __str__(self):
return f"User {self.id}: {self.name}"
main.py¶
# pylint: disable=E0401,E0611
import logging
from models import Users
from sanic import Sanic, response
from tortoise.contrib.sanic import register_tortoise
logging.basicConfig(level=logging.DEBUG)
app = Sanic(__name__)
@app.route("/")
async def list_all(request):
users = await Users.all()
return response.json({"users": [str(user) for user in users]})
@app.route("/user")
async def add_user(request):
user = await Users.create(name="New User")
return response.json({"user": str(user)})
register_tortoise(
app, db_url="sqlite://:memory:", modules={"models": ["models"]}, generate_schemas=True
)
if __name__ == "__main__":
app.run(port=5000)