Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssertionError: slug is defined before trying to ensure uniqueness #21

Open
illagrenan opened this issue Nov 22, 2016 · 3 comments
Open

Comments

@illagrenan
Copy link

I have the exact same issue as described on Bitbucket: https://bitbucket.org/neithere/django-autoslug/issues/45/slug-is-defined-before-trying-to-ensure.

Here is my code (Django 1.10.3 and Python 3.5.2):

class Query(models.Model):
    text = models.CharField(max_length=512, unique=True, db_index=True)
    slug = AutoSlugField(populate_from='text', db_index=True, unique=True, always_update=True)
    # ...

The problem occurs when I try to save Query model with unsluggable data in the text field:

>>> Query.objects.get_or_create(text="???????")
***\lib\site-packages\autoslug\fields.py in pre_save(self, instance, add)
    270
    271         if not self.blank:
--> 272             assert slug, 'slug is defined before trying to ensure uniqueness'
    273
    274         if slug:

AssertionError: slug is defined before trying to ensure uniqueness

The source of the problem is following code in autoslug/utils.py (Note: I have unidecode installed):

# ...

# Use Django's default method over decoded string
    def slugify(value):
        return django_slugify(unidecode(value))

# ...

Django slugify function fails to create slug from text ??????? and autoslug fails on AssertionError.

I don't know how should autoslug behave in these situations but it would be great to provide nicer error (e.g. raise ValueError("Sorry but it is not possible to create slug from...")).

To @egregors: Try to install unidecode: pip install unidecode. Standard slugify function in Django doesn't work on all non-ascii data:

In [1]: from django.template.defaultfilters import slugify

In [2]: slugify('Привет!')
Out[2]: ''

vs:

In [4]: from unidecode import unidecode

In [5]: unidecode('Привет!')
Out[5]: 'Privet!'

Autoslug will use unidecode automatically, see this code: https://github.com/neithere/django-autoslug/blob/master/autoslug/utils.py#L19-L20.

@kostko
Copy link
Contributor

kostko commented Jan 6, 2017

Is this related to #18 and does this PR fix the issue?

@illagrenan
Copy link
Author

Thanks for your message. I'll try it tomorrow and let you know.

@illagrenan
Copy link
Author

I can confirm that the #18 fixes this bug. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants