tags:

views:

35

answers:

2

It works perfectly from the admin site. But the code below doesn't work properly(some characters are missing, like Turkish "ı") in some languages.

class Foo(models.Model):
    name = models.CharField(max_length=50, unique=True, db_index=True)
    slug = models.SlugField(max_length=100, unique=True, db_index=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.name)
        super(Foo, self).save(*args, **kwargs)

For example, let's assume that the name is "ışçğö" and then slug becomes "scgo" when it should be "iscgo" instead.

+1  A: 

This is SlugField behavior by definition. A slug is supposed to be part of a URL. Even though URLs might support non-latin characters, these are not supported inside slugs.

Slug is a newspaper term. A slug is a short label for something, containing only letters, numbers, underscores or hyphens. They're generally used in URLs.


The results you are getting aren't consistent with Django behavior:

>>> from django.template.defaultfilters import slugify
>>> v = u"ışçğö"
>>> slugify(v)
u'isg'

Where exactly are you getting these results?

Yuval A
Thanks. I know, but the admin site turns it into "iscgo" which is right. I just want to make it work the same way.
pocoa
Very strange, I got "iscgo" when I run the code above in console.
pocoa
Hmm, the results might be dependent on display encoding.
Yuval A
+1  A: 

Try the slughifi function for better slug functionality (thanks to Markus for showing me this).

Lexo