Django admin does not allow saving unicode slugs

做~自己de王妃 提交于 2019-12-11 10:58:19

问题


I'm trying to save a Persian slug for this model:

class Category(models.Model):
    name = models.CharField('name', max_length=100)
    slug = models.SlugField('slug', unique=True)
    description = models.TextField('description')

    class Meta:
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    @permalink
    def get_absolute_url(self):
        return ('category_detail', None, {
            'slug': self.slug
        })

    def __unicode__(self):
        return u'%s' % self.name

But Django does not save the page and complaint that:

Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens

I also tried to use this save method instead

    def save(self, *args, **kwargs):
      self.name = slugify_unicode(self.name)
      super(Category, self).save(*args, **kwargs)

but it did not solve the problem. So I got stock on this and appreciate your help to resolve this.


回答1:


You can't. Slug fields can only contain ASCII letters, numbers, dashses and underlines.

If you must use non-ASCII chars in a slug-like field, you can use a normal CharField and add a db_index = True to it.



来源:https://stackoverflow.com/questions/21434959/django-admin-does-not-allow-saving-unicode-slugs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!