Django Custom Field MultilingualTextField

I am trying to create a multilingual text box in Django for my project. I use JSON to store translated text in a database and write my own code in a field to get it in the right language. For instance:

class MyModel(models.Model): text = MultilingualTextField(default_language="en") mymodel = MyModel.objects.create(text="Welcome") mymodel.text.val('fr','Bienvenue') #... mymodel.save() 

when the storage in the database becomes

 {"en":"Welcome","fr":"Bienvenue","es":"Bienvenida"} 

And we can call model.text.val('es') have "Bienvenida".

I use json dumps and json loads to store and retrieve the database value. My question is: is this a good way to do this? Does anyone have a better technique?

0
source share
1 answer

You can watch the Django Model Translation . It does not store data in json, but creates different db columns for each language (for example, text_en, text_fr ..), which, I think, is better.

+1
source

Source: https://habr.com/ru/post/1485732/


All Articles