Is there a way to annotate by a field within the model and another model object?
I have a Restaurant
model object that I want to annotate a 'weight' field. My criteria is number of reviews + number of visits. I would like to do something like this:
weighted = Restaurant.objects.annotate(
weight = Count('reviews') + num_visits??).order_by('weight')
The num_visits part is giving an error and I'm not sure how to correct this. num_visits
is a field in the Restaurant
model. Any help is appreciated.
I think you'll need something like this
qs = Restaurant.objects.annotate(weight=Count('reviews') + F('num_visits'))
weighted = qs.order_by('weight')
It's making use of an F
expression .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.