我处理 FastAPI 并且无法弄清楚如何简化(从我使用 django 的经验的角度来看)事情 - 将带有图像的产品添加到数据库中。据我了解,图像应该存储在静态文件夹中,并且只有指向它的链接应该存储在数据库中。
根据官方文档中的信息,我添加了这个方案
schema.py
from pydantic import BaseModel, HttpUrl
from typing import Optional
class ProductImageSchema(BaseModel):
url: HttpUrl
name: str
class ProductSchema(BaseModel):
name: str
description: Optional[str] = None
image: Optional[ProductImage] = None
我正在使用 SQLAlchemy。现在我的第一个问题是如何正确描述产品型号。模型.py
product = Product(
"product",
metadata,
Column("id", Integer, primary_key=True),
Column("name", String(50)),
Column("description", String(50)),
Column(....str_or_url..????) # field with relation with image,
)
并且不清楚如何创建产品对象的新实例。就像是...crud.py
async def post(payload: ProductSchema, file: UploadFile = File(...)):
content = await file.read()
if file.content_type not in ['image/jpeg', 'image/png']:
raise HTTPException(status_code=406, detail="Only .jpeg or .png files allowed")
file_name = f'{uuid.uuid4().hex}{ext}'
async with aiofiles.open(file_name, "wb") as f:
await f.write(content)
query = product.insert().values(title=payload.name, description=payload.description, image=????????)
return await database.execute(query=query)
最佳答案
你的方法是正确的。据我所知,图像以及其他用户的文件都存储在一个静态(或其他)文件夹中,并在数据库中保存了一个引用。在某些情况下,文件名(可能带有扩展名)存储在表中,而在其他情况下,尤其是在分布式应用程序中,则存储整个 URL。
您的 crud.py
文件似乎是正确的。只需更改 image
查询中的参数到您使用的 URL 或文件名。 请务必检查没有其他用户已插入具有相同名称(和扩展名)的文件,否则您将覆盖您用户的文件!
回到你的问题,
我会进一步完善ProductImageSchema
在 schema.py
文件如下
from datetime import datetime
class ProductImageSchema(BaseModel):
url: HttpUrl
name: str
type: str
creation_date: datetime.datetime = datetime.now()
由于您尚未指定数据模型(即数据库结构),因此我只能根据您编写的内容进行推断。如果我的推断是正确的,一个产品可能有多个图像,所以你需要一个产品表和一个图像表。我的猜测是产品之间不共享图像,否则您将需要 3 个表。虽然这段代码看起来不像有效的 SQLAlchemy 代码,但这里有一个示例,说明您可以做什么
product = Product(
"product",
metadata,
Column("id", Integer, primary_key=True),
Column("name", String(50)),
Column("description", String(50))
)
images = Image(
"image",
metadata,
Column("product", Integer, ForeignKey(Product.id), primary_key=True,),
Column("name", String(50), primary_key=True),
Column("filetype", String(50))
)
但我不确定这是有效的 SQLAlchemy 代码,因为您发布的内容看起来也不正确。无论如何,我希望这足以得到这个想法。
关于python - 如何将图像添加到模型(表)并将其保存到 FastAPI 框架中的数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63017929/