Fully functional implementation of the foreignKey
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
from uuid import UUID, uuid4
|
||||
from enum import Enum
|
||||
|
||||
from sqlmodel import Field, SQLModel, select
|
||||
from sqlmodel import Field, SQLModel, select, Relationship
|
||||
from pydantic import Field as PydField
|
||||
|
||||
from category.models import CategoryRead
|
||||
from category.models import CategoryRead, Category
|
||||
|
||||
|
||||
class AccountType(Enum):
|
||||
@@ -38,6 +38,7 @@ class AccountBaseId(AccountBase):
|
||||
id: UUID | None = Field(default_factory=uuid4, primary_key=True)
|
||||
|
||||
class Account(AccountBaseId, table=True):
|
||||
default_category: Category | None = Relationship()
|
||||
|
||||
@classmethod
|
||||
def create(cls, account, session):
|
||||
@@ -50,7 +51,7 @@ class Account(AccountBaseId, table=True):
|
||||
|
||||
@classmethod
|
||||
def list(cls):
|
||||
return select(Account)
|
||||
return select(Account).join(Category)
|
||||
|
||||
@classmethod
|
||||
def get(cls, session, account_id):
|
||||
@@ -70,8 +71,7 @@ class Account(AccountBaseId, table=True):
|
||||
session.commit()
|
||||
|
||||
class AccountRead(AccountBaseId):
|
||||
# default_category: CategoryRead
|
||||
pass
|
||||
default_category: CategoryRead
|
||||
|
||||
class AccountWrite(AccountBase):
|
||||
default_category_id: UUID = PydField(default=None, json_schema_extra={
|
||||
|
||||
@@ -12,8 +12,8 @@ router = APIRouter()
|
||||
|
||||
@router.post("")
|
||||
def create_account(account: AccountCreate, session: SessionDep, current_user=Depends(get_current_user)) -> AccountRead:
|
||||
Account.create(account, session)
|
||||
return account
|
||||
result = Account.create(account, session)
|
||||
return result
|
||||
|
||||
@router.get("")
|
||||
def read_accounts(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]:
|
||||
|
||||
@@ -4,7 +4,6 @@ from fastapi import APIRouter, HTTPException, Depends
|
||||
from fastapi_filter import FilterDepends
|
||||
from fastapi_pagination import Page
|
||||
from fastapi_pagination.ext.sqlmodel import paginate
|
||||
from pydantic import BaseModel
|
||||
|
||||
from category.models import Category, CategoryCreate, CategoryRead, CategoryUpdate, CategoryFilters
|
||||
from db import SessionDep
|
||||
|
||||
@@ -6,7 +6,7 @@ from fastapi_users import BaseUserManager, FastAPIUsers, UUIDIDMixin, models, ex
|
||||
from fastapi_users.authentication import BearerTransport, AuthenticationBackend
|
||||
from fastapi_users.authentication.strategy.db import AccessTokenDatabase, DatabaseStrategy
|
||||
|
||||
from .models import User, get_user_db, AccessToken, get_access_token_db, UserRead, UserUpdate, UserCreate
|
||||
from user.models import User, get_user_db, AccessToken, get_access_token_db, UserRead, UserUpdate, UserCreate
|
||||
from db import get_session
|
||||
|
||||
SECRET = "SECRET"
|
||||
|
||||
@@ -2,8 +2,8 @@ import uuid
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from .models import User, UserCreate, UserRead, UserUpdate
|
||||
from .manager import get_user_manager, get_current_user, get_current_superuser
|
||||
from user.models import User, UserCreate, UserRead, UserUpdate
|
||||
from user.manager import get_user_manager, get_current_user, get_current_superuser
|
||||
|
||||
from fastapi_pagination import Page
|
||||
from fastapi_pagination.ext.sqlmodel import paginate
|
||||
|
||||
Reference in New Issue
Block a user