2

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?

rahnama7m
  • 865
  • 10
  • 38
  • 2
    You likely have overriden the `slugify` somewhere, by importing something else with the name `slugify`, or defining a function or class in that file with the name `slugify`. – Willem Van Onsem Aug 03 '19 at 16:17
  • 1
    Oh my God! You were right! I mistakenly imported `from django.template.defaultfilters import slugify` and then `from django.utils.text import slugify` in my `models.py` of blog app and in shop app I didn't import `from django.utils.text import slugify.` . Please write your answer to accept that. @WillemVanOnsem – rahnama7m Aug 03 '19 at 16:28

2 Answers2

5

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.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
0

Upgrade Django, that argument allow_unicode introduced in the version 1.9, or call the function without that argument.

ipaleka
  • 3,745
  • 2
  • 13
  • 33