简体   繁体   中英

Problem with Python, FastAPI, Pydantic and SQLAlchemy

I'm trying to build a Python FastAPI blog system using SQLAlchemy with SQLite and am having problems using/understanding the response_model parameter of the API decorator. Here's a SQLAlchemy model:

class User(SqlAlchemyBase):
__tablename__ = 'user'
__table_args__ = {"keep_existing": True}

user_uid: int = sa.Column(GUID, primary_key=True, default=GUID_DEFAULT_SQLITE)
first_name: str = sa.Column(sa.String)
last_name: str = sa.Column(sa.String)
email: str = sa.Column(sa.String, index=True, unique=True)
posts: List["Post"] = relationship("Post", backref="user")
created: datetime = sa.Column(sa.DateTime, default=datetime.now(tz=timezone.utc), index=True)
updated: datetime = sa.Column(sa.DateTime, default=datetime.now(
    tz=timezone.utc), onupdate=datetime.now(tz=timezone.utc), index=True)

Here's the Pydantic schema's for a User:

from datetime import datetime
from pydantic import BaseModel

class UserBase(BaseModel):
    first_name: str
    last_name: str
    email: str

class UserInDB(UserBase):
    user_uid: int
    created: datetime
    updated: datetime

    class Config:
        orm_mode = True

class User(UserInDB):
    pass

Here's a ULR endpoint to get a single user that has the response_model parameter included:

from typing import List
import fastapi
from starlette import status
from backend.schema.user import User
from backend.services import user_service

router = fastapi.APIRouter()

@router.get("/api/users/{user_uid}", response_model=User, status_code=status.HTTP_200_OK)
async def get_one_user(user_uid: str = None) -> User:
    return await user_service.get_user(user_uid)

If I execute the above call in the OpenAPI docs created by FastAPI I get an internal server error in the OpenAPI docs and these errors in the console that's running the FastAPI application:

File "/Users/dougfarrell/projects/blog/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 137, in serialize_response
    raise ValidationError(errors, field.type_)
pydantic.error_wrappers.ValidationError: 6 validation errors for User
response -> first_name
  field required (type=value_error.missing)
response -> last_name
  field required (type=value_error.missing)
response -> email
  field required (type=value_error.missing)
response -> user_uid
  field required (type=value_error.missing)
response -> created
  field required (type=value_error.missing)
response -> updated
  field required (type=value_error.missing)

If I take the response_model parameter out of the @router.get(...) decorator I get back the results of the SQLAlchemy query, but no Pydantic serialization. I don't know what the error stack trace is trying to tell me. Can anyone offer some suggestions, advice or resources that might help me figure out what I'm doing wrong?

I'm using Python version 3.10.1, FastAPI version 0.70.1 and SQLAlchemy version 1.4.29.

Thanks!

Try this:

from typing import List

and change response_model=User to:

response_model=List[User]

You can set the features to optional for every class. For example

from typing import Optional

class UserBase(BaseModel):
    first_name: Optional[str]
    last_name: Optional[str]
    email: Optional[str]
``

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM