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 the TranslatedFields wrapper.

Edit the Product model to add translations for the name, slug, and description fields as follows:

class Product(TranslatableModel):
translations = TranslatedFields(
name = models.CharField(max_length=200, db_index=True),
slug = models.SlugField(max_length=200, db_index=True),
description = models.TextField(blank=True)
)
category = models.ForeignKey(Category,
related_name='products')
image = models.ImageField(upload_to='products/%Y/%m/%d',
blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

django-parler manages translations by generating another model for each translatable model. In the following , you can see the fields of the Product model and what the generated ProductTranslation model will look like:

The ProductTranslation model generated by django-parler includes the name, slug, and description translatable fields, a language_code field, and ForeignKey for the master Product object. There is a one-to-many relationship from Product to ProductTranslation. A ProductTranslation object will exist for each available language of each Product object.

Since Django uses a separate table for translations, there are some Django features that we cannot use. It is not possible to use a default ordering by a translated field. You can filter by translated fields in queries, but you cannot include a translatable field in the ordering Meta options.

Edit the models.py file of the shop application and comment out the ordering attribute of the Category Meta class:

class Category(TranslatableModel):
# ...
class Meta:
# ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'

We also have to comment out the ordering and index_together attributes of the Product Meta class. The current version of django-parler does not provide support to validate index_together. Comment out the Product Meta class as follows:

class Product(TranslatableModel):
# ...

# class Meta:
# ordering = ('-name',)
# index_together = (('id', 'slug'),)

You can read more about django-parler module's compatibility with Django at https://django-parler.readthedocs.io/en/latest/compatibility.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.79.20