views:

26

answers:

1

Suppose I have a Book model with a language field and a foreign key to a Publisher model.

Currently I use a Count annotation in a custom Publisher manager to allow me to add to the admin a sortable column with the number of books by each publisher. (see http://stackoverflow.com/questions/3491766/how-to-add-a-sortable-count-column-to-the-django-admin-of-a-model-with-a-many-to )

My problem now is that I need to have a different column count for the books published in each language.

Is there any way to make the annotation subject to a filter of the related model?

A: 

You can use the double underscore __ for this purpose. Something like this (snippet taken from question linked to by OP):

class PublisherManager(models.Manager):
    def get_query_set(self):
        return super(PublisherManager,self).get_query_set().annotate(lang_count=Count('book__language'))
Manoj Govindan
Thanks Manoj but it doesn't seem to work -- it's counting the books which have *some* language as opposed to null/None.
Gj
Ah. Mea cupla. Didn't test it with actual data.
Manoj Govindan