2

在我的应用程序中,可以通过两种方式删除表“文件”中的行。直接,喜欢session.delete(file)或作为级联删除的结果。当行被删除时,我想从我的文件系统中删除该文件,即数据库行所代表的文件。有什么办法可以做到吗?

我的模型如下所示:

class File(ModelBase):
    """Model for the 'files' table.

    This table holds data about uploaded files.
    """

    __tablename__ = 'files'

    # Columns
    id = Column(INTEGER, primary_key=True)
    uploaded_by_id = Column(INTEGER, ForeignKey(User.__tablename__ + '.id'),
                            nullable=False)
    path = Column(VARCHAR(255), nullable=False) # The file's name on the server
    name = Column(VARCHAR(255), nullable=False) # The original file name
    mime = Column(VARCHAR(75))
    size = Column(INTEGER, nullable=False)   # File size in bytes
    uploaded_at = Column(DATETIME, nullable=False)

    # Relationships
    uploaded_by = relationship('User', backref='files_uploaded')

    def __init__(self, uploaded_by, path, name):

        with ProjectRootContext():
            self.uploaded_by = uploaded_by
            self.path = path
            self.name = name
            self.mime = mimetypes.guess_type(path)[0]
            self.size = os.path.getsize(path)
            self.uploaded_at = datetime.now()

    def __repr__(self):
        return '<File(id=%s,name=%s)>' % (repr(self.id), repr(self.name))

    # Factory function for saving an uploaded file
    @classmethod
    def from_fieldstorage(cls, uploaded_by, fieldstorage):
        # ...
4

1 回答 1

1

您是否尝试过after_delete 映射器事件

于 2012-08-05T18:32:38.037 回答