[Answered ]-Help on my django models for my shoe review website

1๐Ÿ‘

โœ…

You should do:

ShoeReview.objects\
          .filter(owner_review__ratings__rating_attribute__attribute='overall')\
          .order_by(owner_review__ratings__rating)

But actually, you are better off using a manager:

class BestShoesReviewManager(models.Manager):
    def get_query_set(self):
        qs =  super(DahlBookManager, self).get_query_set()
        return qs.filter(owner_review__ratings__rating_attribute__attribute= 'overall')\
                 .order_by(owner_review__ratings__rating)

This way you can do:

class ShoeReview(models.Model)

    objects = models.Manager() # The default manager.
    best = BestShoesReviewManager() # The best review manager.

And so in your code, you can do this:

ShoeReview.objects.best()
๐Ÿ‘คBite code

1๐Ÿ‘

Instead of that:

ShoeReview.objects.filter(owner_review__ratings__rating_attribute = 'overall').sort_by(owner_review__ratings__rating)

you should call:

ShoeReview.objects.filter(owner_review__ratings__rating_attribute__attribute = 'overall').order_by(owner_review__ratings__rating)

owner_review__ratings__rating_attribut expects model (it will retrieve pk from the model and use it in the query). Also there is order_by method, not sort_by. Beside that query seems fine. Try running it and tell, if you get the results or there are some errors. Try running it in ./manage.py shell, this makes it easy and quick to check if it works.

๐Ÿ‘คgruszczy

Leave a comment