Skip to main content

Automatic slug for django

Project description

codecov version downloads license

This package is used to automatically create unique slugs.

Install

pip install django-easy-autoslug

Usage

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 -2, -3

Advanced usage

AutoSlugField arguments are:

  • reserve_chars - number of characters reserved for suffix (including sparator -)

  • title_field - use specific field instread of __str__ method

  • in_respect_to - generate unique slug for specific subset of fields

Following code can create same slug for another month / year.

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')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_easy_autoslug-1.0.4.tar.gz (14.2 kB view hashes)

Uploaded Source

Built Distribution

django_easy_autoslug-1.0.4-py3-none-any.whl (5.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page