Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tortoise-orm custom user #392

Closed
ScrimForever opened this issue Nov 20, 2020 · 10 comments
Closed

Tortoise-orm custom user #392

ScrimForever opened this issue Nov 20, 2020 · 10 comments
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@ScrimForever
Copy link

Iḿ trying make custom user with fastapi_users,

so

from fastapi_users import models
from fastapi_users.db import TortoiseBaseUserModel, TortoiseUserDatabase
import datetime

class User(models.BaseUser):

nome: str = "defaultuserteste"
data_criado: str = datetime.datetime.now()
is_fund: bool = False
is_agendador: bool = False
is_revisor: bool = False
is_aprovador: bool = False

class UserCreate(models.BaseUserCreate):

nome: str = "defaultuserteste"
data_criado: str = datetime.datetime.now()
is_fund: bool = False
is_agendador: bool = False
is_revisor: bool = False
is_aprovador: bool = False

class UserUpdate(User, models.BaseUserUpdate):

nome: str = "defaultuserteste"
data_criado: str = datetime.datetime.now()
is_fund: bool = False
is_agendador: bool = False
is_revisor: bool = False
is_aprovador: bool = False

class UserDB(User, models.BaseUserDB):

nome: str = "defaultuserteste"
data_criado: str = datetime.datetime.now()
is_fund: bool = False
is_agendador: bool = False
is_revisor: bool = False
is_aprovador: bool = False

class UserModel(TortoiseBaseUserModel):

nome: str = "defaultuserteste"
data_criado: str = datetime.datetime.now()
is_fund: bool = False
is_agendador: bool = False
is_revisor: bool = False
is_aprovador: bool = False

user_db = TortoiseUserDatabase(UserDB, UserModel)

Database not changed i'm using mysql ... nothings happening.

@frankie567
Copy link
Member

Did you manage your SQL migrations so that the database structure is updated : https://tortoise-orm.readthedocs.io/en/latest/migration.html ?

@frankie567 frankie567 added the question Further information is requested label Nov 20, 2020
@ScrimForever
Copy link
Author

Yes. But when i show columns inside table, not created then...
I init , init-db, but table not apply new fields

@frankie567
Copy link
Member

I've updated the Tortoise ORM examples, so it should work out-of-the-box. Take care however of the path of your models module:

register_tortoise(
    app,
    db_url=DATABASE_URL,
    modules={"models": ["path_to_your_package"]},  # <<< 🔴 It should be the dotted path of your Python module containing the models
    generate_schemas=True,  # Please also read : https://tortoise-orm.readthedocs.io/en/latest/setup.html?highlight=generate_schemas#tortoise.Tortoise.generate_schemas
)

However, I still strongly recommend to look into a migration system so that schema creation and update is handled properly : https://tortoise-orm.readthedocs.io/en/latest/migration.html

@frankie567 frankie567 added the documentation Improvements or additions to documentation label Nov 21, 2020
@ScrimForever
Copy link
Author

ScrimForever commented Nov 22, 2020

@frankie567

I use exactly as you inform, but my models usuarios, not migration. All others models i can use, all my custom models, just usermodel not create. How can i give more information ?

Im using mysql

@frankie567
Copy link
Member

Could you show us your register_tortoise function call?

@ScrimForever
Copy link
Author

TORTOISE_ORM = {
"connections": {"default": "mysql://xxxx:xxxx@xxx.xxx.xxx.xxx:3306/fund_api"},
"apps": {
"models": {
"models": ["models", "aerich.models"],
"default_connection": "default",
},
},
}

register_tortoise(
app,
#db_url="sqlite://db.sqlite"
#db_url="mysql://xxxxxx:xxxxxxx@xxx.xxx.xxx.xxx:3306/fund_api",
modules={"models": ["models"]},
generate_schemas=True,
add_exception_handlers=True,
config=TORTOISE_ORM,
)

@ScrimForever
Copy link
Author

#Update

The table has been created, so the "columns" that informed on Classes, like:
nome: str = "defaultuserteste"
data_criado: str = datetime.datetime.now()
is_fund: bool = False
is_agendador: bool = False
is_revisor: bool = False
is_aprovador: bool = False

not created.

The table created has name usermodel, with columns (id, email, hashed_password, is_active, is_superuser)

@frankie567
Copy link
Member

OK, I see what happens ; I didn't notice it in your first message. In your Tortoise model, you wrote this:

class UserModel(TortoiseBaseUserModel):
    nome: str = "defaultuserteste"
    data_criado: str = datetime.datetime.now()
    is_fund: bool = False
    is_agendador: bool = False
    is_revisor: bool = False
    is_aprovador: bool = False

This is wrong. Field definitions in Tortoise are not the same as in Pydantic. You should follow the Tortoise way of definining fields. Something like:

class UserModel(TortoiseBaseUserModel):
    nome = fields.CharField(max_length=255)
    data_criado = fields.DatetimeField()  
    is_fund = fields.BooleanField()
    # ...

Be sure to check the getting started document of Tortoise: https://tortoise-orm.readthedocs.io/en/latest/getting_started.html

@ScrimForever
Copy link
Author

@frankie567 .

So sorry, i wrote wrong really. So, i did it as tortoise example, and columns not created too.

If i edit directly on module fastapi-users on class BaseUser(CreateUpdateDictModel):
its works.

@ScrimForever
Copy link
Author

Finally works.
The problem was i not set variable in alls classes and on UserModel i needed add class Config: with orm_mode = True.

Resolved !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants