如何在Django中使用变量作为表名?
我想要做以下事情:
for table in table_list:
'table'.samefield = newvalue
1 个回答
3
如果你知道这个应用程序的名字,那你可以这样做:
model = getattr(application_name.models, 'table')
model.somefield = newvalue
或者你也可以直接这样做:
getattr(application_name.models, 'table').somefield = newvalue
如果你只想用一次这个功能(不过这样做其实没什么意义)。