My endpoint for the user create a item: response_model=Card_Pydantic)Ĭard: CardCreate, user: UserDB = Depends(fastapi_users.get_current_active_user)):Ĭard_obj = await crud.create_user_card(card=card, user=user) I don't know if it is right, I couldn't make it works, but the problem could be in other places. Owner = fields.ManyToManyField("models.UserModel", related_name="cards") I tried to change my owner field and the their relationship to UserModel: class Card(tmodels.Model): ![]() "models.UserModel", related_name="cards") Owner: fields.ForeignKeyRelation = fields.ForeignKeyField( Updated_at = fields.DatetimeField(auto_now=True) Period = fields.CharField(max_length=20, null=True)Ĭreated_at = fields.DatetimeField(auto_now_add=True) Icon = fields.CharField(max_length=3, null=True) State = fields.CharField(max_length=30, null=True) User_db = TortoiseUserDatabase(UserDB, UserModel, OAuthAccountModel) # From FastAPI UsersĬity_name = fields.CharField(max_length=30) ![]() User = fields.ForeignKeyField("models.UserModel", related_name="oauth_accounts")Ĭlass User(models.BaseUser, models.BaseOAuthAccountMixin): # From FastAPI UsersĬlass UserCreate(models.BaseUserCreate): # From FastAPI UsersĬlass UserUpdate(User, models.BaseUserUpdate): # From FastAPI UsersĬlass UserDB(User, models.BaseUserDB, PydanticModel): # From FastAPI Users Models.py class UserModel(TortoiseBaseUserModel): # From FastAPI UsersĬlass OAuthAccountModel(TortoiseBaseOAuthAccountModel): # From FastAPI Users I'm using updated versions of the packages and Python 3.9.4 I tried to learn from Tortoise documentation, but I couldn't find the part I need or understand some complex examples and parts of the documentation. I thought about create an intermediary table, with UserID and CityID only (and an autoincrement ID) and use it to get the related data, but I don't know if it is the best way or it is possible. I think it would be a Many To Many relation, but I don't know how to make the relationship and get it with FastAPI. I don't think it is ideal to repeat the item info (using another ID) for each user, would be better different user access the same item. Then, I want many users having access to the same item (and each user can access multiple items). but I want to have other users accessing the same cities (they have the same info, they are not customized). It was working fine, I receive only the user items (using owner_id), but my item table uses an external unique ID (the item is a city and each city have an unique ID) and it is the primary key.īut, when another user try to add the same city, it gives an error because it cannot have the same city in the table, even with another owner. I want each user to have their items, and after logged and access the endpoint ( response_model=List)), it receives only their items. But now I want some change (an improvement). I was using the foreign key and the relations worked fine. ![]() I have authentication with FastAPI Users. Basically I get cities weather data from OpenWeather API and store in a SQLite database with Tortoise ORM. SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)ĭb: Session = Depends(get_db), token: str = Depends(reusable_oauth2)įollowing approach found in official FastAPI docs, some blogs and also in fastapi_users package documentation: import databasesĪnother way recommended in the new SQLModel package docs by tiangolo himself.I'm using FastAPI, Tortoise ORM and FastAPI Users to make an API to learn. This is found in official FastAPI-fullstack example : from fastapi import DependsĮngine = create_engine(SQLALCHEMY_DATABASE_URI, pool_pre_ping=True) Are there any fundamental differences? if so which approach is preferable? I found two approaches to working with databases in FastAPI.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |