Skip to content

Autoslug populate_from code

Jorik Kraaikamp edited this page Feb 13, 2018 · 1 revision

For django-autoslug to update the slug for each language separately, you should use this code.

from modeltranslation.translator import TranslationOptions


class AutoSlugTranslationOptions(TranslationOptions):
    def add_translation_field(self, field, translation_field):
        if hasattr(translation_field, 'populate_from'):
            translation_field.populate_from = '{}_{}'.format(
                translation_field.populate_from,
                translation_field.language
            )

        super().add_translation_field(field, translation_field)


@register(MyModel)
class MyModelTranslationOptions(AutoSlugTranslationOptions):
    fields = ('name', 'slug')
    required_languages = {'en': ('name', )}
Clone this wiki locally