python - Django: How to annotate query set with count number of active users and non active users -


i'm wondering if there's way add 2 columns django admin display list count of active users , count of non active users, this:

class useradmin(django.contrib.admin.modeladmin): list_display = ('name', 'num_active_user', 'num_non_active_user', )  def num_users(self, obj):     return obj.num_users num_users.short_description = "number of users" num_users.admin_order_field = 'num_users'  def get_queryset(self, request):     qs = super(useradmin, self).get_queryset(request)     return qs.annotate(num_users=count('user', distinct=true)) 

is there way add condition in line this:

return qs.annotate(num_users=count('user__is_active=true', distinct=true)) 

i solve using following trick:

return qs.annotate(nnum_active_users=django.db.models.count(django.db.models.case(django.db.models.when(                                                                                             user__is_active=true, then=1, ))                                                                       )                            ) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -