![]() ![]() 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. get ( "/ " ) def get_user_name ( db : Session = Depends ( get_db ), *, user_id : UUID ) -> str : user = db. ![]() database_uri return FastAPISessionMaker ( database_uri ) app = FastAPI (). ![]() get_db () () def _get_fastapi_sessionmaker () -> FastAPISessionMaker : """ This function could be replaced with a global variable if preferred """ database_uri = DBSettings (). String, nullable = False ) class DBSettings ( BaseSettings ): """ Parses variables from environment on instantiation """ database_uri : str # could break up into scheme, username, password, host, db def get_db () -> Iterator : """ FastAPI dependency that provides a sqlalchemy session """ yield from _get_fastapi_sessionmaker (). Column ( GUID, primary_key = True, default = GUID_DEFAULT_SQLITE ) name = sa. From functools import lru_cache from typing import Iterator from uuid import UUID import sqlalchemy as sa from fastapi import Depends, FastAPI from pydantic import BaseSettings from import declarative_base from sqlalchemy.orm import Session from fastapi_restful.guid_type import GUID, GUID_DEFAULT_SQLITE from fastapi_ssion import FastAPISessionMaker Base = declarative_base () class User ( Base ): _tablename_ = "user" id = sa. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |