gpt4 book ai didi

python - Rails 模型继承

转载 作者:行者123 更新时间:2023-12-01 06:12:30 25 4
gpt4 key购买 nike

用代码示例来解释是最简单的,在Python中我可以这样做来实现模型继承:

"""Image model"""
from sqlalchemy import Column, ForeignKey
from sqlalchemy.types import Integer, String, Text
from miasto_3d.model.meta import Base

class Image(Base):
__tablename__ = "image"
image_id = Column(Integer, primary_key=True)
path = Column(String(200))

def get_mime(self):
#function to get mime type from file
pass



"""WorkImage model"""
class WorkImage(Image, Base):
__tablename__ = "work_images"

image_id = Column(Integer, ForeignKey("image.image_id"), primary_key=True)
work_id = Column(Integer, ForeignKey("work.id"))
work = relation("Work", backref=backref('images',order_by='WorkImage.work_id'))

"""UserAvatar model"""
class UserAvatar(Image, Base):
__tablename__ = "user_avatars"

image_id = Column(Integer, ForeignKey("image.image_id"), primary_key=True)
user_id = Column(Integer, ForeignKey("user.id"))
user = relation("User", backref=backref('images',order_by='UserAvatar.user_id'))

我如何在 Rails 中做类似的事情?或者也许还有另一种更好的方法?我知道回形针,但我不喜欢它使用共享表来存储照片和模型数据的概念。

最佳答案

关于python - Rails 模型继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5019447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com