AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Fastapi sqlite9/10/2023 User_cards = await schemas.Card_om_queryset(Card.filter(owner_id=user.id).all()) My endpoint to get the items owned by the user: response_model=List)Īsync def read_current_user_items(user: UserDB = Depends(fastapi_users.get_current_active_user)): Owner_id=user.id) # ** is used to pass the keys values as kwargs. The CRUD function used (to create an item) here: async def create_user_card(Ĭard: schemas.CardCreate, user: UserDB = Depends(fastapi_users.get_current_active_user)):Ĭard_json = await weather_api.get_api_info_by_name(card.city_name)Ĭard_obj = await Card.create(**card_json, Raise HTTPException(status_code=404, detail=f"City '' not found") 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. I'm using FastAPI, Tortoise ORM and FastAPI Users to make an API to learn.
0 Comments
Read More
Leave a Reply. |