使用Peewee ORM显示已连接的数据

时间:2017-12-14 10:00:01

标签: python sql orm peewee

使用PeeWee Orm我创建了两个表(我已经删除了许多与此查询无关的字段)

产品表

class Product(BaseModel):
    fm_price = FloatField(null=True)
    magento = IntegerField(db_column='magento_id', null=True, index=True)
    sku = CharField(index=True, null=True, unique=True)
    # Compare these two fields to on or offline products
    magento_status_fm = IntegerField(null=True)
    magento_status_ff = IntegerField(null=True)
    # Date fields to put error dates in
    ff_price_error = DateField(index=True, null=True)
    ff_stock_error = DateField(index=True, null=True)
    fm_price_error = DateField(index=True, null=True)
    fm_stock_error = DateField(index=True, null=True)
    ff_check_date = DateField(null=True, index=True)  # When did we get this product from FF

    class Meta:
        db_table = 'products'
        indexes = (
            (('fm_price', 'vip_price', 'education_price'), False),
        )

价格表

class Price(BaseModel):
    price_pk = PrimaryKeyField()
    entity_id = ForeignKeyField(Product, to_field='magento', null=True, on_update='CASCADE', on_delete='CASCADE')
    customer_group_id = IntegerField(null=True)
    final_price = FloatField(null=True)
    min_price = FloatField(null=True)
    check_date = DateField(null=True)

    class Meta:
        indexes = (
            (('entity_id', 'customer_group_id'), True),
        )

示例内容:

产品:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------    ------------------------------------------------------------------------------
| id | CREATION            | MODIFICATION        | fm_price | fm_stock | vip_price | education_price | magento_id | omit | sku             | url                  | web_stock | magento_status_fm | magento_status_ff | ff_price_error | ff_stock_error |     fm_price_error | fm_stock_error | fm_vip_price_error | ff_vip_price_error | ff_check_date |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------    ------------------------------------------------------------------------------
| 3  | 2017-12-12 12:53:01 | 2017-12-14 09:31:44 | 109      | NULL     | NULL      | NULL            | 15212      | NULL | epiea10nach1    | https://www.absolut… | NULL      | NULL              | 1                 | NULL           | NULL           | NULL               | NULL           | NULL               | NULL               | 2017-12-14    |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------    ------------------------------------------------------------------------------
| 5  | 2017-12-12 12:53:01 | 2017-12-14 09:31:44 | 545      | NULL     | NULL      | NULL            | 15433      | NULL | yamapx700mk2blk | https://www.absolut… | NULL      | NULL              | 1                 | NULL           | NULL           | NULL               | NULL           | NULL               | NULL               | 2017-12-14    |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------    ------------------------------------------------------------------------------

价格:

---------------------------------------------------------------------------------------------------------------------------
| price_pk | CREATION            | MODIFICATION | entity_id_id | customer_group_id | final_price | min_price | check_date |
---------------------------------------------------------------------------------------------------------------------------
| 260      | 2017-12-12 12:53:39 | NULL         | 15212        | 0                 | 109         | 109       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 261      | 2017-12-12 12:53:39 | NULL         | 15212        | 1                 | 109         | 109       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 262      | 2017-12-12 12:53:39 | NULL         | 15212        | 2                 | 109         | 109       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 263      | 2017-12-12 12:53:39 | NULL         | 15212        | 3                 | 109         | 109       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 264      | 2017-12-12 12:53:39 | NULL         | 15212        | 4                 | 109         | 109       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 265      | 2017-12-12 12:53:39 | NULL         | 15212        | 5                 | 109         | 109       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 266      | 2017-12-12 12:53:39 | NULL         | 15212        | 6                 | 109         | 109       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 988      | 2017-12-12 12:53:42 | NULL         | 15431        | 0                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 989      | 2017-12-12 12:53:42 | NULL         | 15431        | 1                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 990      | 2017-12-12 12:53:42 | NULL         | 15431        | 2                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 991      | 2017-12-12 12:53:42 | NULL         | 15431        | 3                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 992      | 2017-12-12 12:53:42 | NULL         | 15431        | 4                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 993      | 2017-12-12 12:53:42 | NULL         | 15431        | 5                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 994      | 2017-12-12 12:53:42 | NULL         | 15431        | 6                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 1002     | 2017-12-12 12:53:42 | NULL         | 15433        | 0                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 1003     | 2017-12-12 12:53:42 | NULL         | 15433        | 1                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 1004     | 2017-12-12 12:53:42 | NULL         | 15433        | 2                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 1005     | 2017-12-12 12:53:42 | NULL         | 15433        | 3                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 1006     | 2017-12-12 12:53:42 | NULL         | 15433        | 4                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 1007     | 2017-12-12 12:53:42 | NULL         | 15433        | 5                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------
| 1008     | 2017-12-12 12:53:42 | NULL         | 15433        | 6                 | 545         | 545       | NULL       |
---------------------------------------------------------------------------------------------------------------------------

它们在Magento_Id(magento)字段上连接在一起,该字段是一个整数。 产品表有正确的价格 价格表具有不同客户群的网站上的品红价格

每个产品可能有很多价格,我希望检查价格对我们的内部数据库是否正确(所以网站是正确的)

我写了一个SQL查询,告诉我价格表中的价格不等于Product表中的价格。

我也在PeeWee Orm工作了。

然而,我正在努力让peewee向我展示Price表中的final_price,即使我的SQL查询使用它作为比较器。

示例SQL:

 SELECT
 sku, magento_id, fm_price, price.final_price, `url`
 FROM
 products
 JOIN
 price
 ON
 `products`.`magento_id` = `price`.`entity_id_id` AND `price`.`customer_group_id` = 0
 WHERE
 `products`.`fm_price` != `price`.`final_price`

示例输出:

---------------------------------------------------------------------------------------------------------------
| sku            | magento_id | fm_price | final_price | url                                                  |
---------------------------------------------------------------------------------------------------------------
| bsrhtstudio20h | 17581      | 399      | 449         | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| rolac60rw      | 17697      | 479      | 475         | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| yamthr5        | 17833      | 169.85   | 169         | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| rolgk3b        | 19636      | 155      | 152         | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| rolgk3         | 19763      | 155      | 152         | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| bosac3         | 20115      | 113      | 107.15      | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| bosbd2         | 20128      | 79       | 77.95       | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| bosch1         | 20140      | 77.5     | 71.92       | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------

我的Peewee查询

price_errors = Product.select(Product, Price.final_price).join(Price, on=(Product.magento == Price.entity_id) & (
            Price.customer_group_id == 0)).where(Product.fm_price != Price.final_price)

这给了我正确的结果,但我无法访问Price.final_price来显示

我尝试过以下方法:

for price_errors中的price_error:         打印(price_error.price.final_price)

给出:

AttributeError: 'Product' object has no attribute 'price'

我可以访问price_set,但会显示通过外键加入相关的所有项目,而不是上面指定的加入

我已经为加入和选择添加了别名,但仍然无法获得任何内容。

如果我用

执行它
price_errors = Product.raw("""SELECT id, sku, magento_id, fm_price, 
price.final_price, `url`
                                FROM products
                                JOIN price
                                ON `products`.`magento_id` = 
`price`.`entity_id_id` AND `price`.`customer_group_id` = 0
                                WHERE `products`.`fm_price` != 
`price`.`final_price`""")

然后我可以访问price_error.final_price

但是这种方式无法使用orm :(

1 个答案:

答案 0 :(得分:0)

尝试:

------WebKitFormBoundaryaxaKZBvja5RsdU6h
Content-Disposition: form-data; name="cv"; filename=""
Content-Type: application/octet-stream

------WebKitFormBoundaryaxaKZBvja5RsdU6h