mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2026-03-13 07:49:55 +08:00
Update Beanie docs
This commit is contained in:
@@ -22,23 +22,6 @@ As for any Beanie ODM model, we'll create a `User` model.
|
|||||||
|
|
||||||
As you can see, **FastAPI Users** provides a base class that will include base fields for our `User` table. You can of course add you own fields there to fit to your needs!
|
As you can see, **FastAPI Users** provides a base class that will include base fields for our `User` table. You can of course add you own fields there to fit to your needs!
|
||||||
|
|
||||||
!!! tip "Document ID is a MongoDB ObjectID"
|
|
||||||
Beanie [automatically manages document ID](https://roman-right.github.io/beanie/tutorial/defining-a-document/#id) by encoding/decoding MongoDB ObjectID.
|
|
||||||
|
|
||||||
If you want to use another type, like UUID, you can override the `id` field:
|
|
||||||
|
|
||||||
```py
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
from pydantic import Field
|
|
||||||
|
|
||||||
|
|
||||||
class User(BeanieBaseUser[uuid.UUID]):
|
|
||||||
id: uuid.UUID = Field(default_factory=uuid.uuid4)
|
|
||||||
```
|
|
||||||
|
|
||||||
Notice that `BeanieBaseUser` expects a generic type to define the actual type of ID you use.
|
|
||||||
|
|
||||||
!!! info
|
!!! info
|
||||||
The base class is configured to automatically create a [unique index](https://roman-right.github.io/beanie/tutorial/defining-a-document/#indexes) on `id` and `email`.
|
The base class is configured to automatically create a [unique index](https://roman-right.github.io/beanie/tutorial/defining-a-document/#indexes) on `id` and `email`.
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import motor.motor_asyncio
|
import motor.motor_asyncio
|
||||||
from beanie import PydanticObjectId
|
from beanie import Document
|
||||||
from fastapi_users.db import BeanieBaseUser, BeanieUserDatabase
|
from fastapi_users.db import BeanieBaseUser, BeanieUserDatabase
|
||||||
|
|
||||||
DATABASE_URL = "mongodb://localhost:27017"
|
DATABASE_URL = "mongodb://localhost:27017"
|
||||||
@@ -9,7 +9,7 @@ client = motor.motor_asyncio.AsyncIOMotorClient(
|
|||||||
db = client["database_name"]
|
db = client["database_name"]
|
||||||
|
|
||||||
|
|
||||||
class User(BeanieBaseUser[PydanticObjectId]):
|
class User(BeanieBaseUser, Document):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import motor.motor_asyncio
|
import motor.motor_asyncio
|
||||||
from beanie import PydanticObjectId
|
from beanie import Document
|
||||||
from fastapi_users.db import BeanieBaseUser, BeanieUserDatabase
|
from fastapi_users.db import BeanieBaseUser, BeanieUserDatabase
|
||||||
from fastapi_users_db_beanie.access_token import (
|
from fastapi_users_db_beanie.access_token import (
|
||||||
BeanieAccessTokenDatabase,
|
BeanieAccessTokenDatabase,
|
||||||
@@ -13,11 +13,11 @@ client = motor.motor_asyncio.AsyncIOMotorClient(
|
|||||||
db = client["database_name"]
|
db = client["database_name"]
|
||||||
|
|
||||||
|
|
||||||
class User(BeanieBaseUser):
|
class User(BeanieBaseUser, Document):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class AccessToken(BeanieBaseAccessToken[PydanticObjectId]): # (1)!
|
class AccessToken(BeanieBaseAccessToken, Document): # (1)!
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
import motor.motor_asyncio
|
import motor.motor_asyncio
|
||||||
from beanie import PydanticObjectId
|
from beanie import Document
|
||||||
from fastapi_users.db import BaseOAuthAccount, BeanieBaseUser, BeanieUserDatabase
|
from fastapi_users.db import BaseOAuthAccount, BeanieBaseUser, BeanieUserDatabase
|
||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ class OAuthAccount(BaseOAuthAccount):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class User(BeanieBaseUser[PydanticObjectId]):
|
class User(BeanieBaseUser, Document):
|
||||||
oauth_accounts: List[OAuthAccount] = Field(default_factory=list)
|
oauth_accounts: List[OAuthAccount] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
import motor.motor_asyncio
|
import motor.motor_asyncio
|
||||||
from beanie import PydanticObjectId
|
from beanie import Document
|
||||||
from fastapi_users.db import BaseOAuthAccount, BeanieBaseUser, BeanieUserDatabase
|
from fastapi_users.db import BaseOAuthAccount, BeanieBaseUser, BeanieUserDatabase
|
||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ class OAuthAccount(BaseOAuthAccount):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class User(BeanieBaseUser[PydanticObjectId]):
|
class User(BeanieBaseUser, Document):
|
||||||
oauth_accounts: List[OAuthAccount] = Field(default_factory=list)
|
oauth_accounts: List[OAuthAccount] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import motor.motor_asyncio
|
import motor.motor_asyncio
|
||||||
from beanie import PydanticObjectId
|
from beanie import Document
|
||||||
from fastapi_users.db import BeanieBaseUser, BeanieUserDatabase
|
from fastapi_users.db import BeanieBaseUser, BeanieUserDatabase
|
||||||
|
|
||||||
DATABASE_URL = "mongodb://localhost:27017"
|
DATABASE_URL = "mongodb://localhost:27017"
|
||||||
@@ -9,7 +9,7 @@ client = motor.motor_asyncio.AsyncIOMotorClient(
|
|||||||
db = client["database_name"]
|
db = client["database_name"]
|
||||||
|
|
||||||
|
|
||||||
class User(BeanieBaseUser[PydanticObjectId]):
|
class User(BeanieBaseUser, Document):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user