Search code examples
pythondjangodjango-modelsslugdjango-2.2

slugify() got an unexpected keyword argument 'allow_unicode'


When I want to create new object from product I got this error:

slugify() got an unexpected keyword argument 'allow_unicode'

This is my models:

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)

I also ran the same pattern for other app(blog) ,and there I didn't run into this problem. What's wrong with this app?


Solution

  • Since the slugify function is working in the other apps, it means that you use a different function that, at least in that file is referenced through the slugify identifier. This can have several reasons:

    1. you imported the wrong slugify function (for example the slugify template filter function [Django-doc];
    2. you did import the correct one, but later in the file you imported another function with the name slugify (perhaps through an alias or through a wildcard import); or
    3. you defined a class or function named slugify in your file (perhaps after importing slugify).

    Regardless the reason, it is thus pointing to the "wrong" function, and therefore it can not handle the named argument allow_unicode.

    You can resolve that by reorganizing your imports, or giving the function/class name a different name.