Opened 3 years ago

Closed 3 years ago

#33472 closed Bug (worksforme)

slugify() don't work when used in model's save() method.

Reported by: Vitalii Khrystiuk Owned by: nobody
Component: Utilities Version: 4.0
Severity: Normal Keywords: slugify, utils
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This is code in my models.py I was using:

from django.db import models
from django.utils import timezone
from markdownx.models import MarkdownxField
from .utils import markdownify
from django.contrib.auth.models import User
from django.dispatch import receiver
from django.utils.text import slugify

STATUS = (
        (0,"Draft"),
        (1,"Publish")
)


class Blog(models.Model):
    title = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(max_length=200, unique=True, allow_unicode=False)
    date = models.DateTimeField(auto_now_add=True)
    description = MarkdownxField()
    date_updated = models.DateTimeField(auto_now=True)
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    status = models.IntegerField(choices=STATUS, default=0)

    class Meta:
        ordering = ['-date']

    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
            self.slug = slugify(self.title)
            super().save(*args, **kwargs)

    def formatted_markdown(self):
        return markdownify(self.description)

When model is saved, slug field is just empty.

I've checked if this code is working with "python-slugify" package, and it works, creating a valid slug, so issue is definitely in Django slugify function.

Change History (1)

comment:1 by Mariusz Felisiak, 3 years ago

Resolution: worksforme
Status: newclosed
Summary: Function django.utils.text.slugify don't work when used in model's Save method (and I'm not sure it working elsewhere)slugify() don't work when used in model's save() method.

It works for me, I cannot reproduce your issue with described models.

Note: See TracTickets for help on using tickets.
Back to Top