Answer the question
In order to leave comments, you need to log in
How to sort objects by model function?
the model has a many-to-many relationship, I want to sort this model in the view by the number of related collections.
class Genre(models.Model):
'''Жанр фильма'''
name = models.CharField(max_length=255)
slug = models.SlugField(blank=True)
def __str__(self):
return self.name
def save(self):
self.slug = slugify(unidecode(self.name))
super(Genre, self).save()
def get_count_collections(self):
collections = self.collections_genre.all().count()
return collections
all_genres = Genre.objects.all().order_by('get_count_collections')[:5]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question