Search code examples
pythonpostgresqlormponyorm

how to iterate over ponyorm entity object


I am using ponyORM and i make query to the PeopleModel" for example:

first_name = "avi"
sqlObject = select(p for p in PeopleModel if raw_sql('( lower(first_name) = lower($first_name))

sqlObject object return the list of PeopleModel as expected and its perfect. Now i want to print all PeopleModel values, i expect to something like that to work:

for people_model_key,people_model_value in sqlObject.items():
    print(people_model_value)

But it doesn't working..

How could i print all the people_model values ? Thank you vert much,


Solution

  • The result of select function is a query object:

    first_name = "avi"
    people = select(p for p in PeopleModel
                    if raw_sql('( lower(first_name) = lower($first_name))
    

    It is not a dictionary, so it does not have items method. If you iterate over it, you will get objects. You can access objects attributes in a usual way:

    for person in people:
        print('name:', person.name)
        print('age:', person.age)
    

    If you want to convert object to a dictionary, you can use to_dict method:

    for person in people:
        for key, value in person.to_dict().items():
            print(key, value)