2

我需要实现对实体内容 i18n 的支持。在重构之前,代码如下所示:

class Project (db.Model)
    description = db.TextProperty()

我使用 @property builtin 实现 i18n,以避免重构所有获取或设置 description 属性的代码

class Project (db.Model)
    # English
    description_en = db.TextProperty()

    # Spanish
    description_es = db.TextProperty()

    @property
    def description(self):
        return self.description_es

    @description.setter
    def description(self, value):
        self.description_es = value

当我尝试这样做时,问题出现了:

project = Project(description = "Foo bar")

我使用该语法在大型代码库中初始化模型很多,我无法重构它。但是 db.Model init只设置了 db.properties 没有 @properties。

我尝试覆盖 db.Model 的 init 方法,但出现了很多错误,似乎不建议这样做。

我的问题是:

有没有办法安全地覆盖 init 方法db.Model

是否有其他方法可以实现不意味着对我的代码进行大量重构的内容 i18n?

4

0 回答 0