slugify()得到了意外的关键字参数'allow_unicode'

时间:2019-08-03 16:15:48

标签: python django django-models slug django-2.2

当我想从product创建新对象时,出现此错误:

  

slugify() got an unexpected keyword argument 'allow_unicode'

这是我的模特:

class BaseModel(models.Model):
    created_date = models.DateTimeField(auto_now_add=True)
    modified_date = models.DateTimeField(auto_now=True,)
    slug = models.SlugField(null=True, blank=True, unique=True, allow_unicode=True, max_length=255)
    class Meta:
        abstract = True


class Product(BaseModel):
    author = models.ForeignKey(User)
    title = models.CharField()
     # overwrite your model save method
    def save(self, *args, **kwargs):
        title = self.title
        # allow_unicode=True for support utf-8 languages
        self.slug = slugify(title, allow_unicode=True)
        super(Product, self).save(*args, **kwargs)

我也为其他应用程序(博客)运行了相同的模式,但是我没有遇到这个问题。 此应用程序有什么问题?

2 个答案:

答案 0 :(得分:2)

由于slugify函数可在其他应用程序中使用,这意味着您使用了另一个函数,至少在该文件中,该函数是通过slugify标识符引用的。这可能有几个原因:

  1. 您导入了错误的slugify函数(例如slugify template filter function [Django-doc];
  2. 您确实导入了正确的函数,但是后来在文件中导入了名称为slugify的另一个函数(可能通过别名或通配符导入);或
  3. 您在文件中定义了一个名为slugify的类或函数(可能是在导入slugify之后)。

无论原因为何,它都指向“错误”函数,因此它无法处理命名参数allow_unicode

您可以通过重新组织导入或为函数/类名称指定其他名称来解决此问题。

答案 1 :(得分:0)

升级Django,在版本1.9中引入该参数allow_unicode,或调用不带该参数的函数。

相关问题