Django How to Count in Admin-site without blank

Hello everyone!

By following program, blanks are also counted.
Where should I change to count without blanks?

admin.py

from .models import Inquiry
from django.db.models import Count
from django.contrib import admin

class InquiryAdmin(admin.ModelAdmin):
change_list_template = ‘admin/Inquiry_change_list.html’
date_hierarchy = ‘reception_datetime’

def changelist_view(self, request, extra_context=None):
    response = super().changelist_view(request, extra_context=extra_context, )

    try:
        qs = response.context_data['cl'].queryset
    except (AttributeError, KeyError):
        return response

    metrics = { 'total': Count('customer')  }

    response.context_data['summary6'] = list( qs.values('customer'). annotate(**metrics) )

    response.context_data['summary6_total'] = dict( qs.aggregate(**metrics) )

    return response     

admin.site.register(Inquiry, InquiryAdmin)

Inquiry_change_list.html

{% extends “admin/change_list.html” %}

{% block result_list %}

{% for row in summary6 %} {% endfor %}
{{ row.customer }} {{ row.total }} cases
Total Cases {{ summary6_total.total | default:0 }} cases
{% endblock %}

model.py

from django.db import models
from django.utils import timezone

class Inquiry(models.Model):

customer = models.CharField(verbose_name='Serial No', max_length=100,  blank=True,

def __str__(self):
    return self.subject

What is it that you want counted without blanks?

You show what you’re currently getting, but I’m not understanding what it is that you want to see here.

Hi

even if nothing is entered in charfield, it is still counted.
so I want to count only the fields where characters are entered.

You should be able to exclude the rows from your queryset where the customer field is blank.

So after your try / except block and before the metrics = line you could try something like:

qs = qs.exclude(customer='')