In Django saving a (translated) slug and making it unique doesn't work

362 views Asked by At

I use Django 1.11 and the parler plugin for translation. Every time I save a slug, I wish to

  • test if it already exists
  • truncate the slug
  • add number
  • test again, if the new slug exists and so on

This way, I wish to create a unique slug on saving.

models.py:

from parler.models import TranslatableModel
from django.utils.translation import gettext_lazy as _

class Event(TranslatableModel):
    translations = TranslatedFields(
        event_title=models.CharField(_("event title"), max_length=512),
        slug=models.SlugField(_("slug"), help_text=_("Used in the URL of the event page.")),
        description=RichTextUploadingField(blank=True),
        meta={'unique_together': (('language_code', 'slug'),)},
    )

    def save_translation(self, translation, *args, **kwargs):
        """Create a unique slug of 45 Characters + a dash and 4 digits."""
        translation.slug = translation.slug[:50]
        if Event.objects.active_translations(slug=translation.slug).exists():
            # This is true on the first test for no apparent reason.
            i = 0
            while Event.objects.active_translations(slug=translation.slug).exists():
                translation.slug = translation.slug[:44]+'-'+str(i)
                i += 1
        super(Event, self).save_translation(translation, *args, **kwargs)

This code dosen't work. It always add a number to the slug, no matter what, even if I enter a completely new slug.

1

There are 1 answers

0
mogoh On BEST ANSWER

The problem here is that save_translation gets called multiple times. My solution is:

def save_translation(self, translation, *args, **kwargs):
    """Create a unique slug build of the title + language code + a number."""
    translation.slug = translation.slug[:50]
    """
    This filter is a bit more complicated.
    If the same slug but with a differen ID alread exists:
        ~Q(id=translation.master_id) & 
        Q(translations__slug=translation.slug)
    Or if the same slug with the same ID but different language already exists:
        Q(id=translation.master_id) & 
        ~Q(translations__language_code=translation.language_code) & 
        Q(translations__slug=translation.slug)
    """
    if Event.objects.filter(
            (~Q(id=translation.master_id) &
             Q(translations__slug=translation.slug)) |
            (Q(id=translation.master_id) &
             ~Q(translations__language_code=translation.language_code) &
             Q(translations__slug=translation.slug))
    ).exists():
        i = 1
        while Event.objects.filter(
                (~Q(id=translation.master_id) &
                 Q(translations__slug=translation.slug)) |
                (Q(id=translation.master_id) &
                 ~Q(translations__language_code=translation.language_code) &
                 Q(translations__slug=translation.slug))
        ).exists():
            # Truncate the slug, if it is too long. This happens, if the
            # initial slug is to long or if the trailing number reaches
            # another digit.
            trunc_number = (4 + ceil(log10(i + 1)))
            if len(translation.slug) + trunc_number > 50:
                translation.slug = translation.slug[:50-trunc_number]

            # Substitute the trailing language code and number with a bigger number.
            translation.slug = re.sub(
                r'(?P<slug_start>.*?)(-(de|en))?(-(\d)+)?$',
                '\g<slug_start>' + '-' + translation.language_code + '-' + str(i),
                translation.slug)
            i += 1
    super(Event, self).save_translation(translation, *args, **kwargs)

It got a bit longer then I hoped, but I think it works.