Adding the django-parler to admin pages?

  Kiến thức lập trình

I added django-parler to my django project.

I have added parler in my models.py and that looks like this:

class Category(TranslatableModel):
    translations = TranslatedFields(
        category_name=models.CharField(max_length=255, verbose_name="Category name"),
    )
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE)
    slug = models.SlugField(max_length=200, unique=True, editable=False, default='')

    def clean(self):
        if self.parent:
            if self.parent.parent:
                raise ValidationError("A category that already has a parent cannot be set as the parent of another category.")

    def save(self, *args, **kwargs):
        if not self.slug:
            base_slug = slugify(self.safe_translation_getter('category_name', self.category_name))
            # Ensure uniqueness of the slug
            slug = base_slug
            counter = 1
            while Category.objects.filter(slug=slug).exists():
                slug = f"{base_slug}-{counter}"
                counter += 1
            self.slug = slug
        super(Category, self).save(*args, **kwargs)

    class Meta:
        verbose_name = "Category"
        verbose_name_plural = "Categories"
        ordering = ['category_name']

    def __str__(self):
        return self.safe_translation_getter('category_name', self.category_name)

Tricky part is that I want slug to be translated not just category name.

Also, in admin.py I did it like this:

admin.site.register(Category, TranslatableAdmin)

And I got it like on images below. I’m happy with the result.

enter image description here

enter image description here

What you see on first image, is when you click on categories on admin side, and if I have multiple translations it makes entry for every language. On second image you see how it looks like when you are adding categry.

Now the question, is it possible to add those tabs like I have on second image to add on part what you see in first image?

I appreciate the help, honestly I don’t know how to google this.

LEAVE A COMMENT