0

I have model like following:

class Rule(models.Model):
    business = models.ForeignKey('profiles.Employee', limit_choices_to={'is_employee': True})
    title = models.CharField(max_length=50, blank=False)
    slug = models.SlugField(max_length=50, blank=True, null=True)
    detail = models.TextField(max_length=200)
    frequency = models.CharField(choices=freqs, max_length=10, blank=True, null=True)
    update = models.DateTimeField(auto_now=True)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "{}--{}".format(self.business, self.title)

    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        if not self.title == "":
            self.slug = slugify(self.title)
        super(Rule, self).save()

I don't have any problem with saving English value records in slug but when I wanna save Persian strings I'm encountered with blank field, although I wrote ALLOW_UNICODE_SLUGS = True in the settings.py file. What should I do?

altruistic
  • 896
  • 2
  • 10
  • 23
  • 1
    This question should help : http://stackoverflow.com/questions/702337/how-to-make-django-slugify-work-properly-with-unicode-strings – xyres Aug 15 '15 at 21:21

2 Answers2

2

The new option which is being introduced in django version 1.9 is SlugField.allow_unicode

If True, the field accepts Unicode letters in addition to ASCII letters. Defaults to False. doc

For example:

In models.py file, define the slug column like below:

slug = models.SlugField(allow_unicode=True)
Taher Khorshidi
  • 5,021
  • 5
  • 28
  • 51
Mojtaba Yousefi
  • 538
  • 1
  • 6
  • 25
1

It worked! First of all you should install unidecode by Pip:

pip install unidecode

Or use the following link: https://pypi.python.org/pypi/Unidecode

After that:

def save(self, force_insert=False, force_update=False, using=None,
         update_fields=None):
    from unidecode import unidecode
    from django.template import defaultfilters
    if not self.title == "":
        self.slug = defaultfilters.slugify(unidecode(self.title))
    super(rule, self).save()

It is not pretty good for Persian(Arabic) language but it works!

altruistic
  • 896
  • 2
  • 10
  • 23