Django query annotation with boolean field

前端 未结 6 1147
庸人自扰
庸人自扰 2021-01-31 08:37

Let\'s say I have a Product model with products in a storefront, and a ProductImages table with images of the product, which can have zero or more im

6条回答
  •  滥情空心
    2021-01-31 09:02

    If performance matters, my suggestion is to add the hasPictures boolean field (as editable=False)

    Then keep right value through ProductImage model signals (or overwriting save and delete methods)

    Advantages:

    • Index friendly.
    • Better performance. Avoid joins.
    • Database agnostic.
    • Coding it will raise your django skills to next level.

提交回复
热议问题