Search code examples
djangoslug

Slugfield in tango with django


I'm reading this tutorial to lear the basics of django and I'm facing a problem I can't solve.

I'm at this page http://www.tangowithdjango.com/book17/chapters/models_templates.html at the sluglify function approach.

In the tutorial the author says we have to create a new line in our category model for th slugfield. I folow strictly all the steps just as I show here:

from django.db import models
from django.template.defaultfilters import slugify

class Category(models.Model):
    name = models.CharField(max_length=128, unique=True)
    likes = models.IntegerField(default=0)
    views = models.IntegerField(default=0)
    slug = models.SlugField(unique=True)

    def __unicode__(self):
        return self.name

class Page(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=128)
    url = models.URLField()
    views = models.IntegerField(default=0)


    def __unicode__(self):
        return self.title

When I run the "makemgiration" command everything works as expected: I choose the first option and provide ‘’ . BUT when I run "migrate" I get:

django.db.utils.Integrity error: Slug column is not unique

What is going on here? I've repeated several times the migrations and tried other default codes but with the same ending. I can't figure out what im doing wrong. They only thing left is that i'm giving something else instead of ‘’ (Firstly I thoughtthey were '' or ").

Thankyou for your time and help!


Solution

  • Delete db.sqlite3 and re run

    python manage.py syncdb
    

    Then perform the migrations

    python manage.py makemigrations rango
    python manage.py migrate
    

    and re run your population script. This is a downside of Django that whenever you change models.py the db must be recreated.