假设我有一个名为Visitor的模型。
models.py
class Visitor(models.Model):
name = models.CharField(max_length=100,primary_key=True)
region = models.CharField(max_length=100)
city = models.CharField(max_length=100)
country = models.CharField(max_length=100)
现在我需要做的是
views.py
myView(request):
o = Visitor.objects.get(name='Ankit')
FList = ['region','city','country'] #flist is the list of fields
for myField in FList:
print(o.myField) 'I want to print value of each field for this particular object 'o'.'
我知道print(o.myField)是完全不正确的,因为它只会尝试从Visitor模型中获取此特定对象的“ myField”字段的值,而该模型中不存在“ myField”字段。
我该如何实现?提前致谢。