6👍
✅
If votes possible values are only 1 and -1 you can just sum them using mentioned annotate: Book.objects.annotate(score=Sum('votes__value'))
.
If there is more possible values you can filter annotation by adding .filter(votes__value__range=(1,1))
to the above query.
If it’s more complex you would have to use extra
with select
.
👤ambi
79👍
Raw SQL is not the only way. You can use a Value()
expression (see docs here):
from django.db.models import CharField, Value
MyModel.objects.all().annotate(mycolumn=Value('xxx', output_field=CharField()))
- [Django]-How to get username from Django Rest Framework JWT token
- [Django]-Import error 'force_text' from 'django.utils.encoding'
- [Django]-Determine the requested content type?
Source:stackexchange.com