Abrar Mahi
Abrar Mahi

Reputation: 145

iterate horizontally through a Django QuerySet

I am working with a sql table and want to iterate through it horizontally. I am currently using the django Q library to create query sets:

a preview of the table I am working with

I am creating and filtering the QuerySets by doing :

filtered_table = NewTables07.objects.filter(criterion_date & criterion_location)

The model looks like:

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' 

I am aware that I can iterate through columns by doing something like:

for i in filtered_table:
    print(i.<column title>) 

but if i wanted to iterate through the table horizontally, like through the headers for example : 'ID' then 'Library' 'mono_full' ...

How would I go about doing that ?

Upvotes: 0

Views: 205

Answers (1)

Reza Heydari
Reza Heydari

Reputation: 1211

You can use NewTables07._meta.get_fields() to get all fields in your model and access to field name with field.name

Check this link

Django docs

Upvotes: 1

Related Questions