15

I have a save override method in my model class, which generates a new slug each time an object is saved.

def save(self, *args, **kwargs):
    if self.column2:
        self.slug = slugify(self.column1 + " " + self.column2)
    else:
        self.slug = slugify(self.column1)
    print slug
    super(MyModel, self).save(*args, **kwargs)

When I try to create a new object by logging into the python shell, I see the save method is being invoked.

python manage.py shell

>>> MyModel(column1="test",column2="2015").save()
slug is test-2015

However when I am running a migration, this save override method is not being called. Here's part of my migration script..

...
def add_myModel_details(apps, schema_editor):
    x = apps.get_model("myapp","myModel")
    MyModel(column1 = "test", column2="2015" ).save()
.....

The slug is empty, as the save override isn't being called.

2 Answers 2

11

Custom model methods are not available during migrations.

Instead, you can run put code in your RunPython function that modifies your model instances the way the custom save() would have.

References:

1

It happens because migrations don't call your save method.

I think save method is not the best place for generate slug. Will be better to use AutoSlugField or signals.

1. signals:

In your case you may use signal pre_save.

Example:

@receiver(pre_save, sender=MyModel)
def my_handler(sender, **kwargs):
    my_model = kwargs.get('instance')
    if my_model.column2:
        my_model.slug = slugify(my_model.column1 + " " + my_model.column2)
    else:
        my_model.slug = slugify(my_model.column1)
    print my_model.slug

2. AutoSlugField:

It's not a standard field but a lot of libraries implement it. I use AutoSlugField from django-extensions. This field uses signals too.

Example:

slug = AutoSlugField(populate_from=("column1", "column2"))

3. save method and migrations

But if you still want to use a save method to generating slug I'd recommend you create data migration and add slugs manually.

Data Migrations django >= 1.7

Data Migrations south

5
  • 3
    IMHO the slug field should be assigned in the save() function and in case of changes, it should be manually called after migrations. E.g. ./manage.py shell -> for x in x.objects.all(): x.save() since this allows the code related to saving to be present in the correct function, and the changes to be explicitly applied (in case of errors, etc. they can be detected and resolved) Aug 14, 2015 at 14:51
  • Thanks @Kirill - I will check out the links. AutoSlugField looks interesting. I did take a look at it before and for some reason I can;t remember now, decided it doesn't suit my purpose. Will check the pre_save option. Aug 14, 2015 at 18:28
  • @kicker86 - I got my migration to work by copying the slug logic into the migration as well, but your approach of calling save after the method has more code reusability. Thanks! Aug 14, 2015 at 18:30
  • 2
    First signal approach will not work in data migrations actually. I tried this (Django 1.10), and signals also was not called. See this for details stackoverflow.com/questions/26542617/… Nov 15, 2016 at 12:05
  • According to @kicker86 suggestion we can also implement custom manage.py command for this instead of datamigration, it is very easy: maketips.net/tip/30/django-managepy-command Nov 15, 2016 at 19:06

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.