M
M
MalikDeveloper20772020-02-24 20:40:14
Django
MalikDeveloper2077, 2020-02-24 20:40:14

Django. Why does the model change without slug and not with slugfield?

In general, the data did not change, I decided to remove the slug in the model and it all worked. Before that, when I changed something through the admin panel, and indeed through any tool, nothing changed, but for some reason, when I removed the slugfield, any field changes. Why is this happening?

Models (WAS)

from .for_slug import slugify as my_slugify

class Quiz(models.Model):
    """Quiz model"""
    slug = models.SlugField('Url-адрес', max_length=50, blank=True)
    .....
    
    def save(self, *args, **kwargs):
        """Use the custom slugfiy (for_slug.py)"""
        if not self.slug:
            slug = my_slugify(self.title)
            exists = Quiz.objects.filter(slug=slug).exists()
    
            if exists:
                slug += f'-{str(int(time()))}'
    
            self.slug = slug
            super().save(*args, **kwargs)


for_slug.py
from django.template.defaultfilters import slugify as django_slugify

alphabet = {
    'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd', 'е': 'e',
    'ё': 'yo', 'ж': 'zh', 'з': 'z', 'и': 'i', 'й': 'j', 'к': 'k',
    'л': 'l', 'м': 'm', 'н': 'n', 'о': 'o', 'п': 'p', 'р': 'r',
    'с': 's', 'т': 't', 'у': 'u', 'ф': 'f', 'х': 'kh', 'ц': 'ts',
    'ч': 'ch', 'ш': 'sh', 'щ': 'shch', 'ы': 'i', 'э': 'e', 'ю': 'yu',
    'я': 'ya'
}


def slugify(s):
    return django_slugify(''.join(alphabet.get(w, w) for w in s.lower()))


Because of what this can happen, if I use postgresql

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MalikDeveloper2077, 2020-02-24
@MalikDeveloper2077

The issue is resolved, all due to the fact that I wrote if not self.slug in the save method, I just had to remove this condition.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question