Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

Slug transation issue in wagtail

I've been around the Internet the whole day reading about the following issue, in wagtail if I registered the following model for translation like this:

class RecipientsPage(Page):
intro = RichTextField(null=True, blank=True)
banner_image = models.ForeignKey(
    "wagtailimages.Image",
    on_delete=models.SET_NULL,
    related_name="+",
    null=True,
    blank=False,
    help_text=_("the Image shouldn't exceed ") + "1350 * 210",
)
content_panels = Page.content_panels + [
    FieldPanel("intro"),
    ImageChooserPanel("image"),
]

this is how I registered the model:

@register(RecipientsCountriesPage)
class RecipientsCountriesPage(TranslationOptions):
fields = ("intro",)

It causes a problem, because like this I'll have two slugs following the two titles (The original English one and the Arabic translated one), if I change the Arabic slug manually to equal the English one it'll work, but it's not efficient to do so for each page manually I've read about the issue a lot like in here: https://github.com/infoportugal/wagtail-modeltranslation/issues/195 I've found also the following question with no answer How do you translate the slug value of a page? I've also read that I can override some of the wagtail Page methods but without further explanation and I'm a bit lost, what's the best way to overcome this issue?

question from:https://stackoverflow.com/questions/65874048/slug-transation-issue-in-wagtail

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I did it using Django signals

@receiver(pre_save)
def set_arabic_slug_on_new_instance(sender, instance, **kwargs):
    if isinstance(instance, Page):
        instance.slug_ar = instance.slug_en

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...