from django import template

register = template.Library()


@register.filter
def count_by(queryset, args):
    """
    Filter a queryset by a field and value.
    Usage: {{ compliance_records|count_by:"status:met" }}
    """
    try:
        field, value = args.split(':', 1)
        return queryset.filter(**{field: value}).count()
    except (ValueError, AttributeError):
        return 0
