- Author:
- SmileyChris
- Posted:
- April 8, 2008
- Language:
- Python
- Version:
- .96
- Score:
- 10 (after 10 ratings)
Automatically create a unique slug for a model.
Note that you don't need to do obj.slug = ...
since this method updates the instance's slug field directly. All you usually need is: unique_slugify(obj, obj.title)
A frequent usage pattern is to override the save
method of a model and call unique_slugify
before the super(...).save()
call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 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
|
More like this
- Serializer factory with Django Rest Framework by julio 3 months, 2 weeks ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 4 months, 1 week ago
- Help text hyperlinks by sa2812 5 months ago
- Stuff by NixonDash 7 months, 1 week ago
- Add custom fields to the built-in Group model by jmoppel 9 months, 2 weeks ago
Comments
Looks pretty similar - my way respects maximum length of the slug field, respects the _default_manager rather than assuming "objects".
Oh, and it also handles the case of changing a slug for an existing instance.
I probably should change mine to using a single query, but since it's only at save time for where I use this method, I didn't need it.
#
I just updated the snippet to also allow for changing the slug separator. I needed it to auto-generate a contrib.auth User username which was still editable in admin (doesn't allow the
-
character so I useslug_separator='_'
).#
Hi,
This is an interesting snippet. Does it still work with Django 1.0? Are there any thoughts in post-1.0 to update the slugfield and/or slugify functions to include this functionality?
I was also wondering, if I want to make a unique slug only for a given day would I enter something like this:
#
Thanks for this.
One bug I found:
line 29 should be changed to:
if queryset is None:
otherwise if the argument queryset is empty, it will be re-populated with all rows from the model, leading to slug being modified when it should of been left alone.
#
Good point, seanos. Updated.
#
Good snippet. Maybe add a param that lets the user specify a custom exclude dict in cases where the model doesn't have a pk. But then again, a workaround would be to just specify a query set object that has what you want excluded, excluded. What's this about class.default_manager vs objects? When do I use one vs the other in my own code?
#
eddified: Yep, for fringe cases like that, someone can just explicitly provide the queryset object.
Regarding when to use
._default_manager
- you should use it when referencing potentially third-party model classes since you can't know if they have a manager named "objects".#
Amazing snippet, it really helped me!
#
What's the point of adding
.exclude(pk=instance.pk)
to queryset. If instance.pk is not None, it means that it's an update operation and the field already has a slug. Shouldn't you just return in that case?#
7 years later and this is still useful. Thank you!
#
Please login first before commenting.