Autoslug tries to populate localized AutoSlugField that is not mark as translatable by modeltranslation

Issue #33 resolved
Timothée Mazzucotelli created an issue

You can see in the following code that the slug is populated from title, which is localized, but the slug itself is NOT localized:

# models.py

class Question(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()

    slug = AutoSlugField(populate_from='title', always_update=True, unique=True)

# translation.py

class QTranslation(TranslationOptions):
    fields = ('title', 'description')

translator.register(Question, QTranslation)

Now if you try to create a Question, autoslug fails because it tries to get the field slug_en_us, which does not exist...

Traceback (most recent call last):
File "<console>", line 1, in <module>
File ".../site-packages/django/db/models/base.py", line 589, in save
force_update=force_update, update_fields=update_fields)
File ".../site-packages/django/db/models/base.py", line 626, in save_base
update_fields=update_fields, raw=raw, using=using)
File ".../site-packages/django/dispatch/dispatcher.py", line 198, in send
response = receiver(signal=self, sender=sender, **named)
File ".../site-packages/autoslug/fields.py", line 307, in modeltranslation_update_slugs
field_value = getattr(instance, field_name_localized)
AttributeError: 'Question' object has no attribute 'slug_en_us'

  • django 1.7.7
  • autoslug 1.8.0
  • modeltranslation 0.9.1

Comments (4)

  1. Vašek Dohnal

    I have same issue, Django 1.8.2, modeltranslation 0.9.1 and django-autoslug 1.8.0. As workaround, you can subclass AutoSlugField:

    ########################
    # File: your_app/fields.py
    ########################
    
    from autoslug import AutoSlugField
    
    class NoModelTranslationAutoSlugField(AutoSlugField):
        @staticmethod
        def modeltranslation_update_slugs(sender, **kwargs):
            return None
    
    ########################
    # File: your_app/models.py
    ########################
    
    from your_app.fields import NoModelTranslationAutoSlugField
    
    class Foo(models.Model):
        slug = NoModelTranslationAutoSlugField(populate_from='name', max_length=255, unique=True, db_index=True, always_update=True)
    
        # ....
    
  2. Log in to comment