开发者

How to query for count?

开发者 https://www.devze.com 2023-01-27 04:55 出处:网络
I\'ve got a query, Bid.objects.filter(shipment=shipment, status=BidStatuses.ACCEPTED, user=request.user, items__count=0).exists()开发者_如何转开发

I've got a query,

Bid.objects.filter(shipment=shipment, status=BidStatuses.ACCEPTED, user=request.user, items__count=0).exists()     开发者_如何转开发

The part that doesn't work is items__count=0. Bids have a many-to-many relationship with items. I need to check if this bid has 0 items. How can I do that?


Aggregation.

http://docs.djangoproject.com/en/1.2/topics/db/aggregation/

see the doc upon, read the sample,you will find the answer


For the record (there is already an accepted answer with a link to Django aggregation docs), what OP needs is:

Bid.objects.annotate(item_num=models.Count('items')).filter(shipment=shipment, status=BidStatuses.ACCEPTED, user=request.user, item_num=0).exists()
0

精彩评论

暂无评论...
验证码 换一张
取 消