Nhu Trinh
Nhu Trinh

Reputation: 13956

Custom Django MultilingualTextField model field

I'm trying to create Multilingual text field in Django for my project. I use JSON to store translated text in database and write custom code in field to retrieve it in the right language. For example:

class MyModel(models.Model):
   text = MultilingualTextField(default_language="en")


mymodel = MyModel.objects.create(text="Welcome")
mymodel.text.val('fr','Bienvenue')
#...
mymodel.save()

when store in database it become

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

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

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

Upvotes: 1

Views: 780

Answers (1)

kalo
kalo

Reputation: 408

You can take a look Django Model Translation . It doesn't store the data in json but rather creates different db columns for each language(ex. text_en, text_fr ..) which I think is better.

Upvotes: 1

Related Questions