mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-26 12:31:25 +08:00
Update Tortoise docs and examples
This commit is contained in:
@ -32,14 +32,14 @@ As you can see, **FastAPI Users** provides a mixin that will include base fields
|
|||||||
|
|
||||||
## Create the tables
|
## Create the tables
|
||||||
|
|
||||||
We'll now create an SQLAlchemy enigne and ask it to create all the defined tables.
|
We'll now create an SQLAlchemy engine and ask it to create all the defined tables.
|
||||||
|
|
||||||
```py hl_lines="36 37 38 39 40"
|
```py hl_lines="36 37 38 39 40"
|
||||||
{!./src/db_sqlalchemy.py!}
|
{!./src/db_sqlalchemy.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
!!!tip
|
!!! warning
|
||||||
In production, you would probably want to create the tables with Alembic, integrated with migrations, etc.
|
In production, it's strongly recommended to setup a migration system to update your SQL schemas. See [Alembic](https://alembic.sqlalchemy.org/en/latest/).
|
||||||
|
|
||||||
## Create the database adapter
|
## Create the database adapter
|
||||||
|
|
||||||
|
@ -44,12 +44,15 @@ Notice that we pass a reference to your [`UserDB` model](../model.md).
|
|||||||
|
|
||||||
For using Tortoise ORM we must register our models and database.
|
For using Tortoise ORM we must register our models and database.
|
||||||
|
|
||||||
Tortoise ORM supports integration with Starlette/FastAPI out-of-the-box. It will automatically bind startup and shutdown events.
|
Tortoise ORM supports integration with FastAPI out-of-the-box. It will automatically bind startup and shutdown events.
|
||||||
|
|
||||||
```py hl_lines="33"
|
```py hl_lines="33 34 35 36 37 38"
|
||||||
{!./src/db_tortoise.py!}
|
{!./src/db_tortoise.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
!!! warning
|
||||||
|
In production, it's strongly recommended to setup a migration system to update your SQL schemas. See [https://tortoise-orm.readthedocs.io/en/latest/migration.html](https://tortoise-orm.readthedocs.io/en/latest/migration.html).
|
||||||
|
|
||||||
## Next steps
|
## Next steps
|
||||||
|
|
||||||
We will now configure an [authentication method](../authentication/index.md).
|
We will now configure an [authentication method](../authentication/index.md).
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi_users import models
|
from fastapi_users import models
|
||||||
from fastapi_users.db import TortoiseBaseUserModel, TortoiseUserDatabase
|
from fastapi_users.db import TortoiseBaseUserModel, TortoiseUserDatabase
|
||||||
from tortoise.contrib.starlette import register_tortoise
|
from tortoise.contrib.fastapi import register_tortoise
|
||||||
|
|
||||||
|
|
||||||
class User(models.BaseUser):
|
class User(models.BaseUser):
|
||||||
@ -30,4 +30,9 @@ class UserModel(TortoiseBaseUserModel):
|
|||||||
user_db = TortoiseUserDatabase(UserDB, UserModel)
|
user_db = TortoiseUserDatabase(UserDB, UserModel)
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
register_tortoise(app, modules={"models": ["path_to_your_package"]})
|
register_tortoise(
|
||||||
|
app,
|
||||||
|
db_url=DATABASE_URL,
|
||||||
|
modules={"models": ["path_to_your_package"]},
|
||||||
|
generate_schemas=True,
|
||||||
|
)
|
||||||
|
@ -2,7 +2,7 @@ from fastapi import FastAPI, Request
|
|||||||
from fastapi_users import FastAPIUsers, models
|
from fastapi_users import FastAPIUsers, models
|
||||||
from fastapi_users.authentication import JWTAuthentication
|
from fastapi_users.authentication import JWTAuthentication
|
||||||
from fastapi_users.db import TortoiseBaseUserModel, TortoiseUserDatabase
|
from fastapi_users.db import TortoiseBaseUserModel, TortoiseUserDatabase
|
||||||
from tortoise.contrib.starlette import register_tortoise
|
from tortoise.contrib.fastapi import register_tortoise
|
||||||
|
|
||||||
DATABASE_URL = "sqlite://./test.db"
|
DATABASE_URL = "sqlite://./test.db"
|
||||||
SECRET = "SECRET"
|
SECRET = "SECRET"
|
||||||
@ -30,7 +30,12 @@ class UserModel(TortoiseBaseUserModel):
|
|||||||
|
|
||||||
user_db = TortoiseUserDatabase(UserDB, UserModel)
|
user_db = TortoiseUserDatabase(UserDB, UserModel)
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
register_tortoise(app, db_url=DATABASE_URL, modules={"models": ["test"]})
|
register_tortoise(
|
||||||
|
app,
|
||||||
|
db_url=DATABASE_URL,
|
||||||
|
modules={"models": ["path_to_your_package"]},
|
||||||
|
generate_schemas=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def on_after_register(user: UserDB, request: Request):
|
def on_after_register(user: UserDB, request: Request):
|
||||||
|
@ -8,7 +8,7 @@ from fastapi_users.db import (
|
|||||||
)
|
)
|
||||||
from httpx_oauth.clients.google import GoogleOAuth2
|
from httpx_oauth.clients.google import GoogleOAuth2
|
||||||
from tortoise import fields
|
from tortoise import fields
|
||||||
from tortoise.contrib.starlette import register_tortoise
|
from tortoise.contrib.fastapi import register_tortoise
|
||||||
|
|
||||||
DATABASE_URL = "sqlite://./test.db"
|
DATABASE_URL = "sqlite://./test.db"
|
||||||
SECRET = "SECRET"
|
SECRET = "SECRET"
|
||||||
@ -43,7 +43,12 @@ class OAuthAccountModel(TortoiseBaseOAuthAccountModel):
|
|||||||
|
|
||||||
user_db = TortoiseUserDatabase(UserDB, UserModel, OAuthAccountModel)
|
user_db = TortoiseUserDatabase(UserDB, UserModel, OAuthAccountModel)
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
register_tortoise(app, db_url=DATABASE_URL, modules={"models": ["test"]})
|
register_tortoise(
|
||||||
|
app,
|
||||||
|
db_url=DATABASE_URL,
|
||||||
|
modules={"models": ["path_to_your_package"]},
|
||||||
|
generate_schemas=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def on_after_register(user: UserDB, request: Request):
|
def on_after_register(user: UserDB, request: Request):
|
||||||
|
Reference in New Issue
Block a user