所以我想为一个类似于另一个模型属性的模型添加属性,这个属性包含可翻译的字段。
我的模特:
class MyModel(models.Model):
... fields ...
class MyModelTranslation(models.Model):
... the fields I want on MyModel ...
name = models.CharField()
city = models.CharField()
language = models.CharField()
mymodel = models.ForeignKey(MyModel)
所以我写了以下__init__
class MyModel(models.Model):
...
def __init__(self, *args, **kwargs):
super(Model, self).__init__(*args, **kwargs)
# get_translation gets the object, that holds the translation fields for the
# current active language
trans = lambda c: c.get_translation()
setattr(self.__class__, 'translation', property(trans))
fget = lambda c: getattr(trans(c), 'name')
fset = lambda c, v: setattr(trans(c), 'name', v)
setattr(self.__class__, 'name', property(fget, fset))
fget = lambda c: getattr(trans(c), 'city')
fset = lambda c, v: setattr(trans(c), 'city', v)
setattr(self.__class__, 'city', property(fget, fset))
这完美无缺。当我打电话给mymodel.name
时,我会获得活跃语言的mymodeltranslation.name
。也可以设置此值。
因为我希望动态制作它,所以我可以在mixin中使用它,我将它添加到for循环中,如下所示:
def __init__(self, *args, **kwargs):
super(Model, self).__init__(*args, **kwargs)
trans = lambda c: c.get_translation()
setattr(self.__class__, 'translation', property(trans))
# returns all fields of the translation model
trans_fields = [field.name for field in [
related for related in self._meta.get_all_related_objects()
if "translation" in related.name
][0].model._meta.fields]
# should then assign all those fields dynamically
for name in trans_fields:
if not name == 'id' and not name == 'mymodel':
fget = lambda c: getattr(trans(c), name)
fset = lambda c, v: setattr(trans(c), name, v)
setattr(self.__class__, name, property(fget, fset))
所以我想,好吧,因为name
值类似于我之前手动添加的字段名称,它应该添加property
,其值为{每次运行for循环时都会{1}}。
但事实并非如此。当我调用name
时,它返回模型实例(基本上是mymodel.name
)。我不知道什么是不同的,当我在forloop中而不是全部手动执行时。
有什么想法吗?