Import All Country Languages Using Django Management Command
Today, we are gonna create a simple django management command to import 185 languages with iso code which can be used for many use cases.
We will import from a json file containing the following information structure
[
{
"code": "bn",
"name": “Bengali",
"native": "বাংলা"
}
]
For example your model can be like this
class AllLanguages(TimeStamp):
name = models.CharField(max_length=50)
status = models.BooleanField(default=False)
meta = models.JSONField(blank=True, null=True) def __str__(self):
return str(self.name)
Now create a management command at apps/someapp/management/commands/language.py
Note: We need to create two folders/directories management and commands and both directories must have an empty __init__.py file to work as module.
Also put the languages.json file into the commands directory
[Download the languages.json file here]
Now copy and paste the following code into language.py file
Now just run python manage.py language and the data will be populated on the database.
If you go to admin, you can see all languages. I kept it simple but we should put error handling logic also. Now we can make APIs or anything with it.
Next I will show to import All Countries, States and Cities using one command. Follow me on medium to get more! A clap always inspires