251

I am trying to create a SlugField in Django.

I created this simple model:

from django.db import models

class Test(models.Model):
    q = models.CharField(max_length=30)
    s = models.SlugField()

I then do this:

>>> from mysite.books.models import Test
>>> t=Test(q="aa a a a", s="b b b b")
>>> t.s
'b b b b'
>>> t.save()
>>> t.s
'b b b b'

I was expecting b-b-b-b.

Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33
Johnd
  • 6,441
  • 9
  • 29
  • 22

11 Answers11

457

You will need to use the slugify function.

>>> from django.template.defaultfilters import slugify
>>> slugify("b b b b")
u'b-b-b-b'
>>>

You can call slugify automatically by overriding the save method:

class Test(models.Model):
    q = models.CharField(max_length=30)
    s = models.SlugField()
    
    def save(self, *args, **kwargs):
        self.s = slugify(self.q)
        super(Test, self).save(*args, **kwargs)

Be aware that the above will cause your URL to change when the q field is edited, which can cause broken links. It may be preferable to generate the slug only once when you create a new object:

class Test(models.Model):
    q = models.CharField(max_length=30)
    s = models.SlugField()
    
    def save(self, *args, **kwargs):
        if not self.id:
            # Newly created object, so set slug
            self.s = slugify(self.q)

        super(Test, self).save(*args, **kwargs)
NaturalBornCamper
  • 3,675
  • 5
  • 39
  • 58
Buddy
  • 6,603
  • 1
  • 21
  • 16
  • 4
    shy have a special model type? why not just slugify CharFields? – Johnd May 08 '09 at 03:31
  • 27
    SlugFields set db_index=True by default, and also use a form field by default that has a validation regex to require valid slugs (if represented in a ModelForm or in the admin). You can do those things manually with a CharField if you prefer, it just makes the intention of your code less clear. Also, don't forget the prepopulate_fields ModelAdmin setting, if you want JS-based auto-prepopulate in the admin. – Carl Meyer May 08 '09 at 14:22
  • 4
    As Dingle said below in his answer, you'll need to replace `def save(self):` with `def save(self, *args, **kwargs):` in order to avoid errors from being thrown when writing something like `test.objects.create(q="blah blah blah")`. – Liam Mar 24 '10 at 17:11
  • 6
    Beware that this code will update the slug each saves. your url will change, and "Cool URIs don't change" http://www.w3.org/Provider/Style/URI.html – dzen Jun 03 '11 at 09:37
  • Why not make the slug optional and override `save()` to create slug automatically from title if current slug is empty? `if self.s == '': self.s = slugify(self.q)` – arifwn Dec 10 '11 at 14:32
  • 23
    `slugify()` can also be found in `django.utils.text.slugify`, not clear when this was added. – mrmagooey Feb 10 '13 at 23:54
  • 2
    @mrmagooey it's new in 1.5 ...in 1.4.5 the slugify method is still with the template filters – Anentropic Feb 27 '13 at 12:57
  • What if I have two Test objects with same "q" field? I don't want to have same urls for two different things... – AgentNirmites Jul 01 '21 at 14:14
  • The `slugify` in django.utils.text can use `allow_unicode`, while the one in `django.template.defaultfilters` should be used in templates. – Aris Chow Feb 03 '22 at 06:49
113

There is corner case with some utf-8 characters

Example:

>>> from django.template.defaultfilters import slugify
>>> slugify(u"test ąęśćółń")
u'test-aescon' # there is no "l"

This can be solved with Unidecode

>>> from unidecode import unidecode
>>> from django.template.defaultfilters import slugify
>>> slugify(unidecode(u"test ąęśćółń"))
u'test-aescoln'
DooBLER
  • 1,180
  • 1
  • 7
  • 8
  • 8
    utf-8 is now handled correctly by slugify (in django 1.8.5) – Rick Westera Nov 20 '15 at 23:53
  • As @RickWestera said this is now handled by slugify, although if for some reason you don't want to use slugify, check iri_to_uri from django.utils.encoding: https://docs.djangoproject.com/en/2.0/ref/unicode/#taking-care-in-get-absolute-url – Erwol Mar 24 '18 at 05:53
70

A small correction to Thepeer's answer: To override save() function in model classes, better add arguments to it:

from django.utils.text import slugify

def save(self, *args, **kwargs):
    if not self.id:
        self.s = slugify(self.q)

    super(test, self).save(*args, **kwargs)

Otherwise, test.objects.create(q="blah blah blah") will result in a force_insert error (unexpected argument).

Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33
Dingle
  • 2,402
  • 19
  • 12
  • 2
    One further very minor thing to add to thepeer's answer: I would make that last line `return super(test, self).save(*args, **kwargs)`. I think this method returns `None`, and I don't know of any plans to change that, but it does no harm to return what the superclass's method does in case it changes sometime in the future. – Duncan Parkes Aug 23 '11 at 21:16
  • Please add that *from django.utils.text import slugify* is required for this solution. – Routhinator May 07 '16 at 22:47
  • 1
    @Routhinator did it – Jonas Gröger Aug 19 '16 at 17:33
  • Putting out some feelers to ask if this is still a preferred method for doing this. – sytech Jan 17 '18 at 15:17
31

If you're using the admin interface to add new items of your model, you can set up a ModelAdmin in your admin.py and utilize prepopulated_fields to automate entering of a slug:

class ClientAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('name',)}

admin.site.register(Client, ClientAdmin)

Here, when the user enters a value in the admin form for the name field, the slug will be automatically populated with the correct slugified name.

Yaroslav Admin
  • 13,880
  • 6
  • 63
  • 83
henrym
  • 2,565
  • 1
  • 16
  • 14
  • My `slug` and `name` fields have translations. How can I do that with translations? Because I've tried to add `'slug_en':('name_en',)` and got the error that attribute doesn't exist in my model. – patricia Jul 21 '16 at 12:17
22

In most cases the slug should not change, so you really only want to calculate it on first save:

class Test(models.Model):
    q = models.CharField(max_length=30)
    s = models.SlugField(editable=False) # hide from admin

    def save(self):
        if not self.id:
            self.s = slugify(self.q)

        super(Test, self).save()
spiffytech
  • 6,161
  • 7
  • 41
  • 57
thepeer
  • 8,190
  • 2
  • 18
  • 13
6

If you don't want to set the slugfield to Not be editable, then I believe you'll want to set the Null and Blank properties to False. Otherwise you'll get an error when trying to save in Admin.

So a modification to the above example would be::

class test(models.Model):
    q = models.CharField(max_length=30)
    s = models.SlugField(null=True, blank=True) # Allow blank submission in admin.

    def save(self):
        if not self.id:
            self.s = slugify(self.q)

        super(test, self).save()
Streamweaver
  • 375
  • 2
  • 8
6

Use prepopulated_fields in your admin class:

class ArticleAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("title",)}

admin.site.register(Article, ArticleAdmin)
daaawx
  • 3,273
  • 2
  • 17
  • 16
sergey
  • 61
  • 1
  • 1
4

I'm using Django 1.7

Create a SlugField in your model like this:

slug = models.SlugField()

Then in admin.py define prepopulated_fields;

class ArticleAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("title",)}
markwalker_
  • 12,078
  • 7
  • 62
  • 99
min2bro
  • 4,509
  • 5
  • 29
  • 55
0

You can look at the docs for the SlugField to get to know more about it in more descriptive way.

Ralf
  • 16,086
  • 4
  • 44
  • 68
Sonia Rani
  • 608
  • 9
  • 4
0

You can create slug field with SlugField() and slugify a name field value with slugify(), then save the slugified name field value as a slug field value in overridden save() every time you add and change a Product object as shown below:

# "models.py"

from django.utils.text import slugify

class Product(models.Model):
    name = models.CharField(max_length=20)
    slug = models.SlugField()
    
    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super().save(*args, **kwargs)

And, you can prevent broken links by using id in addition to slug in the url as shown below:

                         # ↓ "slug" ↓
http://myshop.com/product/orange-juice/id
                                 # "id" ↑

So, the url takes id in addition to slug in urls.py as shown below:

# "urls.py"

urlpatterns = [
    path(      # ↓ "slug" ↓ 
        'product/<slug:slug>/<int:id>/', 
        views.get_product, # ↑ "id" ↑
        name="get_product"
    )
]

Then, you should treat only id not treating slug in get_product() in views.py as shown below:

# "views.py"

def get_product(request, id=None, slug=None):
    product_obj = None

    if id:
        product_obj = Product.objects.get(id=id)
    
    return HttpResponse(product_obj)
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
0

In some cases slugify isn't enough standalone. I'm using a different technique for slugs. I think this will cover most scenarios.

First create a slug.py file in your app directory and copy the following codes in it.

slug.py

import re
from django.template.defaultfilters import slugify


def unique_slugify(instance, value, slug_field_name='slug', queryset=None,
                   slug_separator='-'):
    """
    Calculates and stores a unique slug of ``value`` for an instance.
    ``slug_field_name`` should be a string matching the name of the field to
    store the slug in (and the field to check against for uniqueness).
    ``queryset`` usually doesn't need to be explicitly provided - it'll default
    to using the ``.all()`` queryset from the model's default manager.
    """
    slug_field = instance._meta.get_field(slug_field_name)

    slug = getattr(instance, slug_field.attname)
    slug_len = slug_field.max_length

    # Sort out the initial slug, limiting its length if necessary.
    slug = slugify(value)
    if slug_len:
        slug = slug[:slug_len]
    slug = _slug_strip(slug, slug_separator)
    original_slug = slug

    # Create the queryset if one wasn't explicitly provided and exclude the
    # current instance from the queryset.
    if queryset is None:
        queryset = instance.__class__._default_manager.all()
    if instance.pk:
        queryset = queryset.exclude(pk=instance.pk)

    # Find a unique slug. If one matches, at '-2' to the end and try again
    # (then '-3', etc).
    next = 2
    while not slug or queryset.filter(**{slug_field_name: slug}):
        slug = original_slug
        end = '%s%s' % (slug_separator, next)
        if slug_len and len(slug) + len(end) > slug_len:
            slug = slug[:slug_len-len(end)]
            slug = _slug_strip(slug, slug_separator)
        slug = '%s%s' % (slug, end)
        next += 1

    setattr(instance, slug_field.attname, slug)


def _slug_strip(value, separator='-'):
    """
    Cleans up a slug by removing slug separator characters that occur at the
    beginning or end of a slug.
    If an alternate separator is used, it will also replace any instances of
    the default '-' separator with the new separator.
    """
    separator = separator or ''
    if separator == '-' or not separator:
        re_sep = '-'
    else:
        re_sep = '(?:-|%s)' % re.escape(separator)
    # Remove multiple instances and if an alternate separator is provided,
    # replace the default '-' separator.
    if separator != re_sep:
        value = re.sub('%s+' % re_sep, separator, value)
    # Remove separator from the beginning and end of the slug.
    if separator:
        if separator != '-':
            re_sep = re.escape(separator)
        value = re.sub(r'^%s+|%s+$' % (re_sep, re_sep), '', value)
    return value

Then in your models.py import unique_slugify from that file. Then use it in save() method.

models.py

from django.db import models
from yourapp.slug import unique_slugify # Change "yourapp" with your app name

class Test(models.Model):
    q = models.CharField(max_length=30)
    s = models.SlugField()

    def save(self, **kwargs):
        slug = '%s' % (self.q)
        unique_slugify(self, slug)
        super(Test, self).save()
Tech
  • 734
  • 1
  • 6
  • 20