django most efficient way to count same field values in a query

You want something similar to “count … group by”. You can do this with the aggregation features of django’s ORM: from django.db.models import Count fieldname=”myCharField” MyModel.objects.values(fieldname) .order_by(fieldname) .annotate(the_count=Count(fieldname)) Previous questions on this subject: How to query as GROUP BY in django? Django equivalent of COUNT with GROUP BY

Django Query __isnull=True or = None

They are equal: >>> str(Person.objects.filter(age__isnull=True).query) == str(Person.objects.filter(age=None).query) True >>> print(Person.objects.filter(age=None).query) SELECT “person_person”.”id”, “person_person”.”name”, “person_person”.”yes”, “person_person”.”age” FROM “person_person” WHERE “person_person”.”age” IS NULL >>> print(Person.objects.filter(age__isnull=True).query) SELECT “person_person”.”id”, “person_person”.”name”, “person_person”.”yes”, “person_person”.”age” FROM “person_person” WHERE “person_person”.”age” IS NULL Exclusion: the Postgres JSON field (see the answer of @cameron-lee)

Custom QuerySet and Manager without breaking DRY?

The Django 1.7 released a new and simple way to create combined queryset and model manager: class InquiryQuerySet(models.QuerySet): def for_user(self, user): return self.filter( Q(assigned_to_user=user) | Q(assigned_to_group__in=user.groups.all()) ) class Inquiry(models.Model): objects = InqueryQuerySet.as_manager() See Creating Manager with QuerySet methods for more details.