在Django中,保存(翻译的)段并使其唯一不起作用

时间:2018-11-18 11:27:11

标签: django django-models django-parler

我使用Django 1.11和parler插件进行翻译。每次保存a时,我都希望

  • 测试是否已经存在
  • 截断子弹
  • 添加号码
  • 再次测试,是否存在新的子弹,依此类推

这样,我希望在保存时创建一个独特的子弹。

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)

此代码无效。无论输入什么内容,即使输入一个全新的子弹,它总是会在子弹中添加一个数字。

1 个答案:

答案 0 :(得分:0)

这里的问题是save_translation被多次调用。我的解决方案是:

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)

它的寿命比我希望的要长,但是我认为它可行。