简体   繁体   English

Peewee 别名不起作用并抛出 AttributeError

[英]Peewee alias not working and throwing AttributeError

I have a Order model and Payment model.我有一个Order模型和Payment模型。 Payment model has a jsonb column data .付款模型有一个jsonbdata

My Query:我的查询:

orders = (
          Order
          .select(Order, Payment.data.alias('payment_data'))
          .join(Payment, JOIN_LEFT_OUTER, on=(Order.payment==Payment.id))
          .iterator()
         )

When I am iterating over the above query, and accessing order.payment_data , I am getting an AttributeError当我迭代上述查询并访问order.payment_data ,我收到一个AttributeError

But if I write the query below, it gives me the payment_data key in the dict while iterating over the orders:但是,如果我在下面编写查询,它会在迭代订单时在 dict 中为我提供payment_data键:

orders = (
          Order
          .select(Order, Payment.data.alias('payment_data'))
          .join(Payment, JOIN_LEFT_OUTER, on=(Order.payment==Payment.id))
          .dicts()
          .iterator()
         )

Can someone please explain me what I am doing wrong in the first query and how can have access to order.payment_data ?有人可以解释一下我在第一个查询中做错了什么以及如何访问order.payment_data吗?

Thanks谢谢

When I am iterating over the above query, and accessing order.payment_data, I am getting an AttributeError当我迭代上述查询并访问 order.payment_data 时,我收到一个 AttributeError

The payment data is probably getting attached to the related payment instance.付款数据可能会附加到相关的付款实例。 So instead of order.payment_data you would look up the value using:因此,您可以使用以下命令查找值,而不是order.payment_data

order.payment.payment_data

If you want all attributes simply patched directly onto the order, use the objects() query method, which skips the model/relation graph:如果您希望将所有属性直接修补到订单上,请使用objects()查询方法,该方法会跳过模型/关系图:

orders = (Order
          .select(Order, Payment.data.alias('payment_data'))
          .join(Payment, JOIN_LEFT_OUTER, on=(Order.payment==Payment.id))
          .objects()  # Do not make object-graph
          .iterator())
for order in orders:
    print(order.id, order.payment_data)

This is all covered in the docs: http://docs.peewee-orm.com/en/latest/peewee/relationships.html#selecting-from-multiple-sources这都包含在文档中: http : //docs.peewee-orm.com/en/latest/peewee/relationships.html#selecting-from-multiple-sources

This could be a result of having NULL fields in joined results.这可能是连接结果中包含NULL字段的结果。 Probably you miss payment_data for some records and peewee doesn't handle this situation as expected.可能您错过了某些记录的payment_data并且 peewee 没有按预期处理这种情况。

Check if your query results contain NULL s in places of payment_data .检查您的查询结果在payment_data位置是否包含NULL If so you should probably check if order has payment_data attribute on each iteration.如果是这样,您可能应该检查order在每次迭代中是否具有payment_data属性。

Here is more detailed explanation on Github: https://github.com/coleifer/peewee/issues/1756#issuecomment-430399189 Github上有更详细的解释: https : //github.com/coleifer/peewee/issues/1756#issuecomment-430399189

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM