slugify() 收到意外的关键字参数“allow_unicode”

Wha*_*2Hz 2 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)
Run Code Online (Sandbox Code Playgroud)

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

Wil*_*sem 5

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

  1. 您导入了错误的slugify函数(例如slugify模板过滤器函数 [Django-doc]
  2. 您确实导入了正确的函数,但后来在文件中导入了具有该名称的另一个函数slugify(可能通过别名或通配符导入);或者
  3. slugify您定义了在文件中命名的类或函数(可能在 import 之后slugify)。

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

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