如何在Django中使用变量作为表名?

0 投票
1 回答
748 浏览
提问于 2025-04-16 06:05

我想要做以下事情:

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

如果你只想用一次这个功能(不过这样做其实没什么意义)。

撰写回答