快速提问.我正在try 使用变量访问模型的一个字段.
class ExampleModel(models.Model):
the_field = models.CharField()
the_field_two = models.CharField()
如何动态访问该字段?我试过了:
model = ExampleModel.objects.get(pk=1)
fieldtoget = 'the_field'
test_var = model[fieldtoget]
但似乎不管用,你知道我该怎么做吗?
Update:人认为我会更新我的问题.我正在try 编写一个函数(作为较大函数的一部分),它不仅可以获取字段的值,还可以从变量fieldname更新它.例如:
model[fieldtoget] = 'yo'
model.save()
在PHP中,您可以使用{}包装器-$model{$fieldtoget}
-作为示例,因为动态变量名希望在python中有类似的东西:)
干杯