【发布时间】:2013-11-09 20:06:49
【问题描述】:
我有一个像下面这样的模型
Class Product(models.Model):
name = models.CharField(max_length=255)
price = models.IntegerField()
假设我们在数据库中有4 product 记录,有没有办法检查所有4 条产品记录是否都有same price ?
我不想遍历所有产品,因为数据库中可能有thousands的产品记录,这样做会成为性能问题。
所以我正在寻找类似使用内置 django 数据库 ORM 来做到这一点
check_whether_all_the_product_records_has_same_price_value = some django ORM operation......
if check_whether_all_the_product_records_has_same_price_value:
# If all the Product table records(four) has the same price value
# return the starting record
return check_whether_product_has_same_price_value(0)
那么谁能告诉我我们该怎么做?
【问题讨论】:
-
你到底为什么要这样做?它会像疯了一样减慢您的网站速度。
-
ohhhh,实际上有一些功能需要检查上述场景并根据上述场景退回产品,是的,我当然提到它会导致性能问题,所以在这里发布以获得优化如果可能的话解决方案?
标签: python django orm django-queryset