0

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 ?

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Abrar Mahi
  • 65
  • 7

1 Answers1

1

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

Reza Heydari
  • 1,146
  • 1
  • 6
  • 10
  • is there a way to automatically add the fields to my model from my SQL table without having to manually add them? – Abrar Mahi Sep 29 '21 at 01:01