Nuno_147
Nuno_147

Reputation: 2913

django-rating filtering in django

I am trying to do something pretty simple and trivial but with no luck.

I am using django-rating to rate specific objects on my site. On my model which I wanted to rate I have a field :

rating = RatingField(range=5)

Now , all I want is to filter all of the objects which have a rate of 2 and aobve for example.

If rating was IntegerField for example, I would only need to do :

objects.filter( rating__gte = 2)

how can I do the same using django-rating ?

Upvotes: 1

Views: 213

Answers (1)

dani herrera
dani herrera

Reputation: 51675

Reading django-rate documentation I found this trick to sort by rate:

# In this example, ``rating`` is the attribute name for your ``RatingField``
qs = qs.extra(select={
    'rating': '((100/%s*rating_score/(rating_votes+%s))+100)/2' 
               % (MyModel.rating.range, MyModel.rating.weight)
                      })
qs = qs.order_by('-rating')

Perhaps you can modify this code sample and use extra where to get your results:

qs = qs.extra(where=[
         '((100/%s*rating_score/(rating_votes+%s))+100)/2 >= 2 ' % 
          (MyModel.rating.range, MyModel.rating.weight)  ,
                    ])

Upvotes: 1

Related Questions