2

これは最終的にDjangoで可能ですか? この機能がなければ、ORM を使用するのはちょっと奇妙です。

4

2 に答える 2

2

There are actually two sections in the Django aggregation docs called filtering on annotations and order_by() that should get you what you need:

books_w_author_count = Book.objects.annotate(num_authors=Count('authors'))

# just a filter by number of objects
books_w_author_count.filter(num_authors__gt=1)

# just ordering on the count
books_w_author_count.order_by('num_authors')

class Author(modules.Model):
   # ...

class Book(models.Model):
   # ...
   authors = models.ManyToManyField(Author)
于 2012-02-02T17:06:32.660 に答える
1

You can this by using anotate function. Here you can find an example

And docs here

于 2012-02-02T17:05:41.600 に答える