Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Latest commit

 

History

History
60 lines (41 loc) · 1.28 KB

README.rst

File metadata and controls

60 lines (41 loc) · 1.28 KB

Automatic slug generation from content

Install

pip install https://github.com/mireq/django-autoslugfield.git

Usage

Settings

INSTALLED_APPS = (
        # ...
        'django_autoslugfield',
)

Basic example

# models.py

from django_autoslugfield import AutoSlugField

class Item(models.Model):
        title = models.CharField(max_length=255)
        slug = AutoSlugField(max_length=255, unique=True)

Slug is created from __str__ method. If another object with same slug already exists slug will be suffixed with number.

Advanced usage

AutoSlugField arguments are:

  • reserve_chars - number of characters reserved for suffix
  • title_field - use specific field instread of __str__ method
  • in_respect_to - generate unique slug for specific subset of fields (or not
    unique when in_respect_to contains pk)
from django_autoslugfield import AutoSlugField

class Blog(models.Model):
        title = models.CharField(max_length=255)
        slug = AutoSlugField(filter_fields=('year', 'month'), max_length=255)
        year = models.IntegerField()
        month = models.IntegerField()

        class Meta:
                unique_together = ('slug', 'year', 'month')