【发布时间】:2016-10-30 20:17:03
【问题描述】:
我想通过字符串名称访问我在 django 应用程序中定义的模型及其属性。 我找到了这两个解决方案,但它们不适合我的问题。
How to access object attribute given string corresponding to name of that attribute
Python: call a function from string name
例如: 模型.py
Class Foo(models.Model):
var1 = models.CharField(max_length=20)
var2 = models.CharField(max_length=20)
现在,我有“Foo.var2”字符串,我想访问 Foo 模型并在其 var2 字段中进行过滤。
【问题讨论】:
-
你是怎么得到那个字符串的?