2👍
✅
You can use a defaultdict instead:
from collections import defaultdict
def catatlog(request):
cars = defaultdict(list)
for i in Car.objects.all():
cars[i.name].append(i.model)
return render(request, 'index.html', {'cars':cars})
Source:stackexchange.com