Peewee 外键从一个 table 到不同的表

Peewee foreign key from one table to different tabels

我正在使用 peewee 开发简单的资产管理器数据库。每个资产可以属于以下类型之一:图像或视频。

假设它看起来像这样:

# MAIN ASSET
class Asset(Model):
    name = CharField()
    datetime = DateTimeField()
    miniature = CharField()
    preview = CharField()
    type_ = ForeignKeyField()

    class Meta:
        database = DB

# TYPE SPECIFIC
class Image(Model):
    resolution_x = IntegerField()
    resolution_y = IntegerField()
    channels = CharField()
    layers = IntegerField()

    class Meta:
        database = DB

class Video(Model):
    resolution_x = IntegerField()
    resolution_y = IntegerField()
    framerate = FloatField()
    bitrate = FloatField()
    duration = FloatField()

    class Meta:
        database = DB

我想将所有资产保存在一个 table 中,将资产特定字段保存在另一个中,并将它们与“type_”字段连接(例如使用外键或其他)。我怎样才能做到这一点?我认为外键是一种不好的方法,因为它需要一个预先确定的模型。

看起来您正在尝试创建一个 Multiple Table Inheritance. Maybe this 话题可能会有帮助