通过 Django QuerySet 水平迭代
iterate horizontally through a Django QuerySet
我正在使用 sql table 并希望水平迭代它。我目前正在使用 django Q 库创建查询集:
我正在创建和过滤查询集,方法是:
filtered_table = NewTables07.objects.filter(criterion_date & criterion_location)
模型看起来像:
class NewTables07(models.Model):
TestDate = models.CharField(max_length=200)
Division = models.CharField(max_length=200)
Library = models.CharField(max_length=200)
ID = models.CharField(max_length=200)
mono_full = models.CharField(max_length=200)
mono_simple = models.CharField(max_length=200)
mono_brief = models.CharField(max_length=200)
mono_complex = models.CharField(max_length=200)
mono_vendor = models.CharField(max_length=200)
mono_asc = models.CharField(max_length=200)
mono_added = models.CharField(max_length=200)
class Meta:
db_table = 'stat_cat1'
我知道我可以通过执行以下操作来遍历列:
for i in filtered_table:
print(i.<column title>)
但是如果我想水平地遍历 table,比如通过 headers:'ID' 然后 'Library' 'mono_full' ...
我该怎么做?
您可以使用 NewTables07._meta.get_fields()
获取模型中的所有字段并使用 field.name
访问字段名称
Check this link
我正在使用 sql table 并希望水平迭代它。我目前正在使用 django Q 库创建查询集:
我正在创建和过滤查询集,方法是:
filtered_table = NewTables07.objects.filter(criterion_date & criterion_location)
模型看起来像:
class NewTables07(models.Model):
TestDate = models.CharField(max_length=200)
Division = models.CharField(max_length=200)
Library = models.CharField(max_length=200)
ID = models.CharField(max_length=200)
mono_full = models.CharField(max_length=200)
mono_simple = models.CharField(max_length=200)
mono_brief = models.CharField(max_length=200)
mono_complex = models.CharField(max_length=200)
mono_vendor = models.CharField(max_length=200)
mono_asc = models.CharField(max_length=200)
mono_added = models.CharField(max_length=200)
class Meta:
db_table = 'stat_cat1'
我知道我可以通过执行以下操作来遍历列:
for i in filtered_table:
print(i.<column title>)
但是如果我想水平地遍历 table,比如通过 headers:'ID' 然后 'Library' 'mono_full' ...
我该怎么做?
您可以使用 NewTables07._meta.get_fields()
获取模型中的所有字段并使用 field.name
Check this link