วิธีรับคอลัมน์ / ฟิลด์ด้วยแบบสอบถาม peewee

สำหรับรุ่น

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

และรายการช่อง lst = ['phone', 'name', 'points']

มีวิธีรับคำค้นหาของคุณเพื่อส่งคืนฟิลด์ใน lst หรือไม่

ฉันไม่พบตัวอย่างในเอกสาร แต่ ดูเหมือนว่า ORM ของ Django จะมีประมาณ ...get().values(lst)

ฉันพยายามส่งรายการเป็นอาร์กิวเมนต์ไปที่ User.select() แต่ได้รับ

TypeError: issubclass() arg 1 must be a class

ฉันเดาว่าฉันสามารถทำบางอย่างเช่น [getattr(obj, field) for field in lst] กับวัตถุผลลัพธ์ได้ แต่ดูเหมือนว่าจะมีวิธีที่ดีกว่านี้ใช่ไหม

อัปเดต: ลิงก์ไปยัง values ในเอกสารของ Django คือ ที่นี่


person beardc    schedule 04.06.2013    source แหล่งที่มา


คำตอบ (6)


คุณสามารถใช้ได้:

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

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

person coleifer    schedule 08.07.2013
comment
ขอบคุณ ฉันใช้วิธีนั้นอยู่ (จริงๆ แล้วบางอย่างเช่น User.select(*[getattr(User, f) for f in lst])) แต่สงสัยว่ามีวิธีส่งรายการสตริงของชื่อฟิลด์โดยตรง เช่น วิธี values ของ Django หรือไม่ ฉันจะอัปเดตคำถามพร้อมลิงก์ไปยังเอกสารที่ฉันอ้างถึง - person beardc; 09.07.2013

คุณสามารถใช้ได้:

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

User.select(*lst)

ฉันใช้วิธีนี้เพื่อเลือกฟิลด์แบบไดนามิก SELECT จากรายการที่กำหนดไว้ล่วงหน้า

person user3468635    schedule 27.03.2014

ไม่แน่ใจว่าปัญหาของคุณคืออะไร แต่คุณลองใช้ object_list หรือไม่ ด้วยการใช้ขวด-peewee ?

def user_query():

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

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

ฉันขอโทษถ้าช่วยได้ไม่มาก

ความนับถือ.

person hepidad    schedule 04.07.2013
comment
ขอบคุณ แต่ดูเหมือนว่านี่จะเป็นเทมเพลตของ Flask ไม่ใช่เหรอ (ซึ่งผมไม่ได้ใช้) - person beardc; 05.07.2013

นี่เป็นเรื่องเก่า แต่ฉันพบคำตอบเมื่อเร็ว ๆ นี้

เมื่อกำหนดอินสแตนซ์ u จากผู้ใช้ตาราง จากนั้น u._data จะส่งคืนพจนานุกรมที่มีชื่อฟิลด์เป็นคีย์

ตัวอย่างเช่น:

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
person upandacross    schedule 24.08.2016

วิธีแก้ไขคือใช้ tuples หรือ dicts

เช่นสำหรับสิ่งอันดับจากเอกสาร:

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

เช่นสำหรับคำสั่งจากเอกสาร:

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']

โปรดจำไว้ว่าผลลัพธ์เป็นตัววนซ้ำ ดังนั้นหากคุณต้องการให้ทูเพิลหรือ dict ทั้งหมดอยู่ในหน่วยความจำ คุณต้องเริ่มต้นมันด้วยวิธีเริ่มต้น: tuple(stats) หรือ dict(stats)

ข้อมูลเพิ่มเติมที่นี่

person Willemoes    schedule 14.08.2017

ข้อมูลประเภทนี้ถูกซ่อนอยู่ในข้อมูลเมตาของโมเดล คุณสามารถเข้าถึงได้ดังนี้:

User._meta.fields.keys()

สิ่งนี้จะส่งคืนวัตถุ dict_keys หากคุณต้องการให้รายการเพียงแค่แยกวิเคราะห์ -› list()

person SaulVP    schedule 03.02.2021