I am using an a django package called django-countries which gives me access to some countries attributes in the world such as country name, country code, country flag and some other more.
It seems the default appears to be the country code, i will like to know how to be able to use the country name instead of the default which is the country code.
models.py
from django.db import models
from django_countries.fields import CountryField
class Product(models.Model):
name = models.CharField(max_length=36)
price = models.PositiveIntegerField()
country = CountryField(blank_label='(select country)')
For example in my views, i have a code like this
def analyse_market(request):
qs = Product.objects.values('country').annotate(
number=Count('pk')
).order_by('country')
result = {
q['country']: q['number']
for q in qs
}
print(result)
context = {"result":result}
return render(request, 'core/analyse-market.html', context)
This return a result like this:
{'AD': 3, 'AR': 5, 'BH': 1, 'FR': 1, 'JP': 1, 'NG': 1, 'NL': 1}
In the documentation, they have different methods used such as country.name, country.code, country.flag, I have tried the country.name because it relates to what I need, but i get KeyError when using the country.name in my views.