A
A
Alex Xmel2021-03-15 11:37:03
Django
Alex Xmel, 2021-03-15 11:37:03

How to automatically generate a slug from a Russian word?

there is a simple model:

class ModelZavet(models.Model):
    mz_name = models.CharField('Название для сайта', max_length=200)
    mz_slug = models.SlugField('URL', max_length=70, db_index=True)
    mz_num = models.PositiveSmallIntegerField('Порядок', default=1)
    mz_descriptions = models.TextField('Описание', blank=True)


in admin.py it is written accordingly:
prepopulated_fields = {'mz_slug': ('mz_name',)}

everything works fine, the slug is automatically created. No complaints.
But here the thought arises that one should be able to create entries in the database not only through the admin panel, but also through the shell. In this regard, the question arises - how then will the slug be created? because it should be created automatically

, the following code is added to the model:
def save(self, *args, **kwargs):
        if not self.id:
            self.mz_slug = slugify(self.mz_name)
        super(ModelZavet, self).save(*args, **kwargs)

and everything works. I create a new entry through the shell, I don’t set the slug, then I call the save method and see that there is a new entry in the database and there is an automatically created slug there.
But, if the mz_name field from which the slug is created is written in Russian, then the slug is not created. Accordingly, everything crashes because the record becomes not unique and other heaps of problems. Why is this only with a Russian name, what should I fix?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dr. Bacon, 2021-03-15
@bacon

write your own slugify with Russian support, or, before that, make a transliteration of mz_name

V
Vadim Shatalov, 2021-03-15
@netpastor

https://github.com/un33k/python-slugify

V
VictorSeverlock, 2021-03-16
@VictorSeverlock

from autoslug import AutoSlugField # AutoSlugField needs to be additionally installed (pip install django-autoslug) - adds slug fields
from uuslug import uuslug # needs to be additionally installed (pip install django-uuslug) - translates URLs to Latin
class ModelZavet(models.Model):
...
mz_slug = AutoSlugField('URL', max_length=70, db_index=True, unique=True, populate_form=lambda instance: instance.mz_name, slugify=lambda value: value.replace(' ', '-'))
. ..
def save(self, *args, **kwargs):
self.mz_slug = uuslug(self.mz_slug, instance=self)
super(ModelZavet, self).save(*args, **kwargs)
# populate_form -> takes title title
# slugify=lambda value: value.replace(' ', '-') -> replace spaces with dashes
! In order to avoid problems when performing migration -> replace lambda functions with regular functions and write them before the model
def instance_ mz_slug(instance):
return instance.mz_name
def slugify_value(value):
return value.replace(' ', '-' ) #should
be like this:
class ModelZavet(models.Model):
...
mz_slug = AutoSlugField('URL', max_length=70, db_index=True, unique=True, verbose_name='URLs',
populate_from= instance_ mz_slug, slugify= slugify_value)
...
! If admin.py says
prepopulated_fields = {'mz_slug': ('mz_name',)} -> need to be removed or commented out
! There is no need to specify anything else

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question