Question

I have a carousel which consists of a Gallery model that has several Picture models. The Picture model looks like this:

class Picture(models.Model):
    gallery = models.ForeignKey(Gallery)
    image = models.ImageField(upload_to="uploads/images/")
    title = models.CharField(max_length=30, null=True, blank=True)
    tagline = models.CharField(max_length=30)
    description = models.CharField(max_length=100)
    page_link = PageField(related_name="Page", null=True, blank=True)

As the website has 3 languages, how can the admin use the same carousel for all languages, having the same image, but different translatetions for texts?

There has to be a better way than just using title_en, title_de, etc, right?

Was it helpful?

Solution

I would highly recommend django-hvad.

Here's your models.py as an hvad TranslatableModel:

class Picture(TranslatableModel):
    translations = TranslatedFields(
        title = models.CharField(max_length=30, null=True, blank=True)
        tagline = models.CharField(max_length=30)
        description = models.CharField(max_length=100)
    )
    gallery = models.ForeignKey(Gallery)
    image = models.ImageField(upload_to="uploads/images/")
    page_link = PageField(related_name="Page", null=True, blank=True)

    def __unicode__(self):
        return self.lazy_translation_getter('title', self.pk)

As mentioned in the docs, this app is still in Beta but I've used it in many production environments and works good.

One thing to keep in mind is that it does bring a few limitations.

The way it works is it creates a model called PictureTranslation (or something like that) which will have your three fields title, tagline, description and a ForeignKey to Picture.

You can still do picture_instance.title as long as the picture_instance is language aware.

If you would like, I can give a better explanation based on your specific use case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top