python - Django, ModelManager with a query based on model property -


i have models.py:

class piecemanager(models.manager):     def top_ten(self):         # return self.get_queryset().order_by(total_likes)         # how can first n-items ordered property total_likes?   class piece(models.model):      name = models.charfield(max_length=250)     description = models.textfield(blank=true, null=true)     museum = models.foreignkey(museum, blank=true, null=true)      objects = piecemanager()      @property     def total_likes(self):         return self.likes.count()   class like(models.model):      created_at = models.datetimefield(auto_now_add=true, blank=true, null=true)     piece = models.foreignkey(piece, blank=true, null=true, related_name="likes") 

how can first -items ordered total_likes property? right way that? there better way? thanks.

as simple search have shown, can't sort result of property or method.

however in case don't need to, since can better , more efficiently aggregation:

from django.db.models import count  self.annotate(likes_count=count('like')).order_by('likes_count') 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -