brsbilgic
brsbilgic

Reputation: 11833

Django sort the list - complex query

These are my models

class Review(models.Model):
    reviewHeader = models.CharField(null=False,default="",max_length=200)

class ReviewRate(models.Model):
    review = models.ForeignKey(CompanyReview,null=False,default=0)
    isPositive = models.BooleanField(blank=True)

I would like to sort the reviews by the number of positive votes.

Review.objects.all().annotate(ss = Sum('rev__reviewrate__isPositive')).order_by('-ss')

This sorts the reviews by which has the maximum positive votes. But if a review has 2 positive and 2 negative votes, it comes before the review which has only one positive vote.

But I would like to sort them by (positive-negative) count.

How can I achieve this ?

Thanks

Upvotes: 2

Views: 106

Answers (1)

soField
soField

Reputation: 2686

for this kind of things i think you need to add new field to your model like num_of_pos_votes then you can sort with this field

Upvotes: 1

Related Questions