Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Generate slug field data for existing entries in database

Tags:

Some time ago I created a Django model:

class Product(models.Model):
    name = models.CharField(verbose_name=_('Nome'), max_length=100)
    description = models.CharField(verbose_name=_('Descrizione'), blank=True, default="")

Now I would like to insert a slug field:

class Product(models.Model):
    name = models.CharField(verbose_name=_('Nome'), max_length=100)
    slug = AutoSlugField(populate_from='name', unique=True)
    description = models.CharField(verbose_name=_('Descrizione'), blank=True, default="")

My problem is that when I create a migration, Django asks me to insert a default value for the slug field.

My idea is to generate a slug during the migration for existing entries in database, there is a way to do it?

Thanks!

like image 969
marco.santonocito Avatar asked May 18 '16 20:05

marco.santonocito


People also ask

How do I add a slug field in Django?

Now, we will add SlugField to our project. So, it automatically generates the URLs according the title. Create a file called util.py in the project level directory. We have to generate URL every time invoke the Post model.

Why should we use slug in Django?

Slug is used in Django to dynamically generate a human-friendly/readable URL.

What is slug type in Django?

What is SlugField in Django? It is a way of generating a valid URL, generally using data already obtained. For instance, using the title of an article to generate a URL. Let's assume our blog have a post with the title 'The Django book by Geeksforgeeks' with primary key id= 2.


1 Answers

this is the code I have for the same situation.

models.py:

slug = AutoSlugField(null=True, default=None, unique=True, populate_from='name')

Note the null=True which is compatible with a unique field In my migrations, I'm adding a data migration by manually editing the migration file

0007_my_migration.py:

def migrate_data_forward(apps, schema_editor):
    for instance in MyModel.objects.all():
        print "Generating slug for %s"%instance
        instance.save() # Will trigger slug update

def migrate_data_backward(apps, schema_editor):
    pass

class Migration(migrations.Migration):
...

    operations = [
        migrations.AddField(
            model_name='my_model',
            name='slug',
            field=autoslug.fields.AutoSlugField(null=True, default=None, editable=False, populate_from='name', unique=True),
            preserve_default=False,
        ),
        migrations.RunPython(
            migrate_data_forward,
            migrate_data_backward,
        ),
    ]
like image 142
raphv Avatar answered Nov 25 '22 12:11

raphv