[英]How to get columns/fields with peewee query?
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']
和字段列表, lst = ['phone', 'name', 'points']
Is there a way to get your query to return the fields in lst
?有没有办法让您的查询返回lst
的字段?
I can't find an example in the docs , but it seems like Django's ORM has something like ...get().values(lst)
.我在docs 中找不到一个例子,但似乎 Django 的 ORM 有类似...get().values(lst)
。
I tried passing the list as an argument to User.select()
, but get我尝试将列表作为参数传递给User.select()
,但得到
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?我想我可以对结果对象执行类似[getattr(obj, field) for field in lst]
,但似乎应该有更好的方法?
Update : The link to values
in Django's docs is here .更新:Django 文档中values
的链接是here 。
You can use:您可以使用:
User.select(User.phone, User.name, User.points)
http://peewee.readthedocs.org/en/latest/peewee/api.html#SelectQuery http://peewee.readthedocs.org/en/latest/peewee/api.html#SelectQuery
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.我使用这种方法从预定义的列表中动态SELECT
字段。
Not sure what your problem is, but did you try using object_list , with using flask-peewee ?不确定您的问题是什么,但是您是否尝试使用object_list和使用 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.问候。
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.给定一个来自表 User 的实例 u,然后 u._data 返回一个以字段名称作为键的字典。
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
The solution to this is to use tuples
or dicts
.解决这个问题的方法是使用tuples
或dicts
。
Eg 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
Eg for dicts from the docs:例如,对于文档中的 dicts:
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)
.请记住,结果是迭代器,因此如果您想将整个元组或字典保存在内存中,则必须使用默认方法对其进行初始化: tuple(stats)
或dict(stats)
。
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()如果你想要一个列表,这将返回一个 dict_keys 对象,只需解析它 -> list()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.