adrian oviedo
adrian oviedo

Reputation: 684

django-parler translations for abstract model

My django app must be translatable, static pages and models too. For translating models I'm using django-parler app. This works fine, but for simple models, I mean, models that doesn't inherits from an abstract model class.

Let's say that we have a Vehicle abstract model

class Vehicle(TranslatableModel):

    translations = TranslatedFields(
        description=models.CharField(max_length=100)
    )

    class Meta:
        abstract = True

and a child model which is Car:

class Car(Vehicle)
    """..."""

This raised me this error: TypeError: Can't create TranslatedFieldsModel for abstract class Vehicle.

I'd like still using django model inheritance. So, what can I do for translating my models using django-parler, it support translations for abstract models or I'll need to use another app to achieve this, in that case any suggestion on any?

Upvotes: 3

Views: 1875

Answers (2)

p14z
p14z

Reputation: 1810

The best solution is to use TranslatedField as explained in the docs: https://django-parler.readthedocs.io/en/latest/api/parler.fields.html#the-translatedfield-class

Upvotes: 0

user2390182
user2390182

Reputation: 73498

The problem is, parler implicitly creates an extra db table for the translations which has a ForeignKey to the model that the translated fields are declared on. You cannot have a FK to an abstract model because it does not have its own db table. What if you have two models subclassing this abstract model? Which table is the translation table's FK pointing to?

You could try to implement the translatable fields outside of your model if you want to share the same translatable fields across models and still be relatively DRY:

vehicle_translations = TranslatedFields(
    description=models.CharField(max_length=100)
)

class Car(TranslatableModel):
    translations = vehicle_translations

Upvotes: 0

Related Questions