coderholic / django-cities

Countries and cities of the world for Django projects
MIT License
930 stars 374 forks source link

How do I use localised country names? #201

Closed TurnrDev closed 4 years ago

TurnrDev commented 6 years ago

Checklist

fffffrolov commented 5 years ago

My version:

  1. Using custom model (myapp.City)
  2. Add field name_local
  3. create migration/command:
    cities = City.objects.filter(name_local__isnull=True)
    for instance in cities:
    lang = instance.country.language_codes.split(',')[0]
    if lang:
        alt_name = instance.alt_names.filter(language_code=lang).order_by('-is_preferred').first()
        name_local = alt_name.name if alt_name else instance.name
        City.objects.filter(pk=instance.pk).update(name_local=name_local)
  4. Same for Country