Add utf-8 support in slugs with autoslug

Slugs end up empty if list name consists only of non-ascii characters
and that results in an error. Autoslug converts non-ascii characters to
appropriate ascii ones and auto-updates the slug when list name changes.

Add unidecode dependency and fix PEP8 errors

models.py: add one blank line for PEP8
This commit is contained in:
Manos Pitsidianakis 2017-04-10 14:30:24 +03:00
parent f235b4effc
commit 710cb1a0ad
2 changed files with 3 additions and 8 deletions

View file

@ -99,6 +99,7 @@ setup(
include_package_data=True, include_package_data=True,
zip_safe=False, zip_safe=False,
tests_require=['tox'], tests_require=['tox'],
install_requires=['django-autoslug', 'unidecode', ],
cmdclass={ cmdclass={
'clean': Clean, 'clean': Clean,
'test': Tox, 'test': Tox,

View file

@ -3,24 +3,18 @@ import datetime
from django.db import models from django.db import models
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from django.template.defaultfilters import slugify
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
from django.conf import settings from django.conf import settings
from autoslug import AutoSlugField
@python_2_unicode_compatible @python_2_unicode_compatible
class List(models.Model): class List(models.Model):
name = models.CharField(max_length=60) name = models.CharField(max_length=60)
slug = models.SlugField(max_length=60, editable=False) slug = AutoSlugField(populate_from='name', editable=False, always_update=True)
group = models.ForeignKey(Group) group = models.ForeignKey(Group)
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.name)
super(List, self).save(*args, **kwargs)
def __str__(self): def __str__(self):
return self.name return self.name