Search code examples
pythondjangodjango-modelsslug

Django prepopulated_fields removes stop words from slugs?


I am trying to automatically generate slugs to use in URLs of a Django app.

So far I have achieved this using prepopulated_fields in admin.py.

However, slugs generated with prepopulated_fields do not include stop words (i.e., the string "I love to code" has slug "love-code").

Is there a way to automatically generate slugs that also include stop words?


Solution

  • Slug is generated on fronted using prepopulate.js function and does not have any configuration options, you could add your custom javascript instead

    Or

    forget about preopopulated_fields and instead override save method on Headword model and generate slug using slugify() something in a line of

    from django.utils.text import slugify
    
    class Headword(models.Model):
        ...
    
        def save(self, *args, **kwargs):
    
            self.slug = slugify(self.headword, allow_unicode=True)
            super().save(*args, **kwargs)