Home / Expert Answers / Computer Science / djangoi-have-this-model-and-i-am-using-a-django-package-for-star-rating-that-uses-this-model-the-two-pa928

(Solved): DJANGOi have this model and i am using a django package for star rating that uses this model the two ...



DJANGO

i have this model

and i am using a django package for star rating that uses this model Exploring Computer Science
Count:
Total:
Average:
Content type:
Object id:

the two models are connected by the id that is created by default for every django model, where photo has the id field and the Rating table has the Object id field that i assume referenced this. I am trying to create a view that will order the photos by the average rating rather than just by the order that they are entered into the database.

here is the view i currently use to display them by default

class PhotoListView(ListView):

model = Photo

template_name = 'photoapp/list.html'

context_object_name = ‘photos’

i am unsure wether i need a new view or if i need some sort of loop in my html files that orders them by average rating. Any help would be appreciated.

Exploring Computer Science Count: Total: Average: Content type: Object id:


We have an Answer from Expert

View Expert Answer

Expert Answer



Answer- To order the photos by average rating, you need to modify the PhotoListView class. One way to achieve this is by annotating the queryset with the average rating and then ordering by that field. Here's the code:
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe