我想知道是否有可能使下面的函数成为泛型的,这样它就可以遍历任何给定的模型,并且不需要像下面的函数那样在该模型中声明所有字段
def CustomerTable(ExampleModel):
CustomerInfo = ExampleModel.objects.all()
CustomerJson = []
for customers in CustomerInfo:
CustomerJson.append({
'ID': customers.ID,
'Title': customers.Title,
'Name': customers.Name,
'Description': customers.Description,
'Location': customers.Location,
'DateAdded': customers.DateAdded,
})
CustomerTable = {'Records' :CustomerJson}
Return HttpResponse (CustomerTable)
谢谢你的帮助
看看这个帖子:Django: Get list of model fields?
您可以调用更多的方法,请查看官方文档:https://docs.djangoproject.com/en/1.8/ref/models/meta/#retrieving-all-field-instances-of-a-model
可以将模型作为参数传递给函数:
然后您可以在
但是,字段的映射并不精确(外键的fieldname\u id)
所以,正如程的回答中已经提到的,最好使用
相关问题 更多 >
编程相关推荐