How to get columns/fields with peewee query?
Asked Answered
L

6

14

For a model

class User(db.Model, BaseUser):
    name = CharField()
    phone = CharField()
    age = IntegerField()
    points = IntegerField()

and a list of fields, lst = ['phone', 'name', 'points']

Is there a way to get your query to return the fields in lst?

I can't find an example in the docs, but it seems like Django's ORM has something like ...get().values(lst).

I tried passing the list as an argument to User.select(), but get

TypeError: issubclass() arg 1 must be a class

I guess I could do something like [getattr(obj, field) for field in lst] with a resulting object, but seems there should be a better way?

Update: The link to values in Django's docs is here.

Lindo answered 4/6, 2013 at 21:19 Comment(0)
J
22

You can use:

User.select(User.phone, User.name, User.points)

http://peewee.readthedocs.org/en/latest/peewee/api.html#SelectQuery

Jennie answered 8/7, 2013 at 20:31 Comment(1)
Thanks, I've been using that way (actually, something like User.select(*[getattr(User, f) for f in lst])) but was wondering if there's a way to directly pass a list of strings of field names like Django's values method. I'll update the question with a link to the docs I'm referring to.Lindo
W
5

You can use:

lst = [User.phone, User.name, User.points]

User.select(*lst)

I use this method to choose the fields dynamically SELECT from a predefined list.

Waiver answered 27/3, 2014 at 13:1 Comment(0)
D
2

The solution to this is to use tuples or dicts.

E.g for tuples from the docs:

stats = Stat.select(Stat.url, fn.Count(Stat.url)).group_by(Stat.url).tuples()

# iterate over a list of 2-tuples containing the url and count
for stat_url, stat_count in stats:
    print stat_url, stat_count

E.g for dicts from the docs:

stats = Stat.select(Stat.url, fn.Count(Stat.url).alias('ct')).group_by(Stat.url).dicts()

# iterate over a list of 2-tuples containing the url and count
for stat in stats:
    print stat['url'], stat['ct']

Remember that the results are iterators, so if you want the whole tuple or dict in memory you must initialize it with the defaults methods: tuple(stats) or dict(stats).

More info here

Dasteel answered 14/8, 2017 at 9:50 Comment(0)
S
1

Not sure what your problem is, but did you try using object_list, with using flask-peewee ?

def user_query():

   lst_result = User.select().where(User.name == lst.name)

   return object_list('yourtemplate.html', lst_result, 'list_user')

I am sorry if not much help.

Regards.

Schlessinger answered 4/7, 2013 at 6:53 Comment(1)
Thanks, but it looks like this is for Flask templates no? (which I don't use)Lindo
N
1

This is an old one, but I found an answer recently.

Given an instance u from table User, then u._data returns a dictionary with field name as keys.

For example:

db.connect() # db was established with connection parameters for your database
u = User.get() # get one record
print u._data # get dictionary with field names as keys
db.close() # free up your connection
Nikolaos answered 24/8, 2016 at 15:18 Comment(0)
I
0

This kind of data is hidden in the metadata of the Model, you can access it like this:

User._meta.fields.keys()

This will return a dict_keys object if you want a list just parse it -> list()

Impresa answered 3/2, 2021 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.