Translating model fields

Let's add translations for our product catalog. django-parler provides a TranslatedModel model class and a TranslatedFields wrapper to translate model fields. Edit the models.py file inside the shop application directory and add the following import:

from parler.models import TranslatableModel, TranslatedFields

Then, modify the Category model to make the name and slug fields translatable as follows:

class Category(TranslatableModel):    translations = TranslatedFields(        name = models.CharField(max_length=200,                                db_index=True),        slug = models.SlugField(max_length=200,                                db_index=True,                                unique=True)    )

The Category model now inherits from TranslatedModel instead of models.Model and both the name and slug fields are included in ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.