Kristof Dochez
Kristof Dochez

Reputation: 290

get attribute names of class python/ get column names of query

I want to have the column names of my sqlalchemy query but I do not find how I can do this.

I tried a few ways

first way I have tried was to make a new class, because the names had to be different of the column names

class DosierWeergave():
    def __init__(self, code_arg, year_registr, period_registr, staynum, status):
        self.Code_Arg = str(code_arg)
        self.Year_registr = str(year_registr)
        self.Period_registr = str(period_registr)
        self.Staynum = str(staynum)
        self.Status = str(status)

And then I maked an object of this

a = DosierWeergave(187,2010,4,"0000010000", "inbehandeling")

I get then the right names but not in the right order. Does anybody know how I can get the columnnames (class attributes) in the right order.

Another way was to use the function: columns.keys()

HeaderDosiers = Dosiers2.__table__.columns.keys()

but her I got all the column headers and I do not find how I can filter them has anybody an idea how I can fix this problems or has anybody got a better way to do it

Upvotes: 1

Views: 1283

Answers (1)

Michel Keijzers
Michel Keijzers

Reputation: 15357

What do you want to do with the column names; if you want to print them, create a str function:

def __str__(self):
    str  = 'Code Arg', self._code_arg
    str += 'Year_registr', self.Year_registr
    ...

    return str

I would not suggest using reflection.

Upvotes: 1

Related Questions