just started fooling around with Django and came across a link here on how to create slugs. I was told to perform the following changes to an existing model:
from django.template.defaultfilters import slugify
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
views = models.IntegerField(default=0)
likes = models.IntegerField(default=0)
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Category, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
This worked out pretty well until I tried to migrate the database using:
python manage.py makemigrations
The above asked for a default value so following the guide, I gave it ''. Then:
python manage.py migrate
The above returned "DETAIL: Key (slug)=() is duplicated."
I'm not entirely sure why this happened. Perhaps it's because I'm adding a new field that is unique and I can't populate it with ''? If so, what do I have to do in order to populate the database?
The documentation explains how to migrate in these circumstances. A quick summary:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With