Files
François Voron 104a6c6bf5 Inject every models variations and DB model in DB adapters (#84)
* Inject every model variations in router and DB model in DB adapters

* Update documentation and import Tortoise in db module

* Use path operation decorator dependencies for superuser routes
2020-01-04 15:36:34 +01:00

1.7 KiB
Raw Blame History

User model

FastAPI Users defines a minimal User model for authentication purposes. It is structured like this:

  • id (str) Unique identifier of the user. Default to a UUID4.
  • email (str) Email of the user. Validated by email-validator.
  • is_active (bool) Whether or not the user is active. If not, login and forgot password requests will be denied. Default to True.
  • is_active (bool) Whether or not the user is a superuser. Useful to implement administration logic. Default to False.

Define your models

There are four Pydantic models variations provided as mixins:

  • BaseUser, which provides the basic fields and validation ;
  • BaseCreateUser, dedicated to user registration, which makes the email compulsory and adds a compulsory password field ;
  • BaseUpdateUser, dedicated to user profile update, which adds an optional password field ;
  • BaseUserDB, which is a representation of the user in database, adding a hashed_password field.

You should define each of those variations, inheriting from each mixin:

from fastapi_users import models


class User(models.BaseUser):
    pass


class UserCreate(User, models.BaseUserCreate):
    pass


class UserUpdate(User, models.BaseUserUpdate):
    pass


class UserDB(User, models.BaseUserDB):
    pass

You can of course add you own properties there to fit to your needs!

Next steps

Depending on your database backend, database configuration will differ a bit.

I'm using SQLAlchemy

I'm using MongoDB

I'm using Tortoise ORM