1👍
Please do not use a global
variable: you each time assign a new value to contact
: the first time it is the contact
model, the next time it is a queryset. Furthermore, global state [softwareengineering] is a severe antipattern, especially in web servers. Work with:
from app_name.models import Contact
@login_required(login_url='common:login')
def contact_list(request):
targets = Contact.objects.values('target').annotate(
count=Count('email')
).order_by('target')
context = {'contact': targets}
Note: Models in Django are written in PascalCase, not snake_case,
so you might want to rename the model fromtocontact
Contact
.
Source:stackexchange.com