【问题标题】:Get ordered field names from peewee Model从 peewee 模型中获取有序的字段名称
【发布时间】:2013-05-29 23:48:55
【问题描述】:

我想使用 peewee 到 csv 中的 create 记录。看起来语法需要关键字 args:

user = User.create(username='admin', password='test')

如果 csv 中的行看起来像 (admin, test),那么知道字段名称 ('username', 'password') 以形成要传入的 dict 会很方便(看起来我无法传入模型来推断相应的字段是什么)。

User 的属性是否具有按定义顺序排列的字段名称列表?

谢谢

【问题讨论】:

    标签: python orm peewee


    【解决方案1】:

    好像是User._meta.get_field_names()

    我刚刚看到有人在another question提到它。

    【讨论】:

    • 自我注意:不要使用User作为模型名称
    • 我只是偶然发现了这个问题:为什么不使用 User 作为模型名称?
    • 这是一个reserved word in postgres。 Peewee 可以很好地处理它,但它使使用控制台变得更加棘手。
    • 对于那些偶然发现这个问题的人的更新:对于较新版本的peewee,您需要使用User._meta.sorted_fieldsUser._meta.sorted_field_names
    • 我不得不使用<class>._meta.fields.keys(),因为sorted_field_names(有时)没有返回外键。
    猜你喜欢
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 2016-03-26
    • 2018-06-11
    • 2013-11-06
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    相关资源
    最近更新 更多