Files
nanocode/new-media-platform/website-new/backend/app/models/image.py
T
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

40 lines
1.6 KiB
Python

"""
图片资源模型
复用自 personal-blog,添加 module 字段支持多模块共用
"""
from sqlalchemy import Column, Integer, String, DateTime, func
from app.database import Base
class Image(Base):
"""上传的图片资源"""
__tablename__ = "images"
id = Column(Integer, primary_key=True, index=True)
filename = Column(String(255), nullable=False, comment="存储文件名")
original_name = Column(String(255), comment="原始文件名")
file_path = Column(String(500), nullable=False, comment="原图路径")
thumb_path = Column(String(500), comment="缩略图路径")
description = Column(String(500), comment="图片描述")
file_size = Column(Integer, comment="文件大小(字节)")
width = Column(Integer, comment="图片宽度")
height = Column(Integer, comment="图片高度")
module = Column(String(20), default="news", comment="所属模块:news/product/page等")
created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
def to_dict(self):
"""转换为字典"""
return {
"id": self.id,
"filename": self.filename,
"original_name": self.original_name,
"file_path": self.file_path,
"thumb_path": self.thumb_path,
"description": self.description,
"file_size": self.file_size,
"width": self.width,
"height": self.height,
"module": self.module,
"created_at": self.created_at.isoformat() if self.created_at else None,
}