[英]Django orm, transform object name
I can not figure out how to convert the name of the object on the fly我不知道如何即时转换 object 的名称
LANGUAGES = [ 'en','af', 'ar','az', 'bg','be','bn','bs',]
for i in LANGUAGES:
r = myCity.objects.get(id=tet.id)
r.myCity_base_ (+i) =(str(z.name))
r.save
You should get something like this object type standard django orm你应该得到这样的 object 类型标准 django orm
r.myCity_base_en
r.myCity_base_af
r.myCity_base_az
r.myCity_base_bg
thank you for your help谢谢您的帮助
use module django-modeltranslation使用模块 django-modeltranslation
You can work with setattr(…)
[Django-doc] for this:您可以为此使用setattr(…)
[Django-doc] :
for i in LANGUAGES:
r = myCity.objects.get(id=tet.id)
setattr(r, f'myCity_base_{i}', str(z.name))
r.save()
setattr(x, 'y', z)
is equivalent to xy = z
. setattr(x, 'y', z)
等价于xy = z
。
but it is unclear to me where the z
and tet
originate from.但我不清楚z
和tet
的来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.