python - Slug error in django -
i new django , learning basics. however, stuck slug module.
hope find solution. in advance.
while running migration command
python manage.py makemigrations rango python manage.py migrate
i keep getting error:
django.db.utils.integrityerror: unique constraint failed: rango_category__new.slug
models.py file
from django.db import models 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 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
the tutorial says "since did not provide default value slug, , have existing data in model, migrate command give 2 options. select option provide default, , enter ‘’. dont worry updated shortly. re-run "populate.py" script. since save method called each category, overrided save method executed, updating slug field. run server, , inspect data in models via admin interface." (link of tutorial mentioned in point 3 @ bottom)
populate.py file
import os os.environ.setdefault('django_settings_module', 'kiran_blog_project.settings') import django django.setup() exodus.models import category, page def populate(): python_cat = add_cat('python',128,64) add_page(cat=python_cat, title="official python tutorial", url="http://docs.python.org/2/tutorial/",views=20) add_page(cat=python_cat, title="how think computer scientist", url="http://www.greenteapress.com/thinkpython/", views=19) add_page(cat=python_cat, title="learn python in 10 minutes", url="http://www.korokithakis.net/tutorials/python/",views=17) django_cat = add_cat("django",64,32) add_page(cat=django_cat, title="official django tutorial", url="https://docs.djangoproject.com/en/1.5/intro/tutorial01/",views=30) add_page(cat=django_cat, title="django rocks", url="http://www.djangorocks.com/",views=21) add_page(cat=django_cat, title="how tango django", url="http://www.tangowithdjango.com/",views=50) frame_cat = add_cat("other frameworks",32,16) add_page(cat=frame_cat, title="bottle", url="http://bottlepy.org/docs/dev/",views=13) add_page(cat=frame_cat, title="flask", url="http://flask.pocoo.org",views=16) # print out have added user. c in category.objects.all(): p in page.objects.filter(category=c): print "- {0} - {1}".format(str(c), str(p)) def add_page(cat, title, url, views): p = page.objects.get_or_create(category=cat, title=title)[0] p.url=url p.views=views p.save() return p def add_cat(name,views,likes): c = category.objects.get_or_create(name=name)[0] c.views=views c.likes=likes c.save() return c # start execution here! if __name__ == '__main__': print "starting population script..." populate()
so question is,
1.after selecting option provide default value value should enter?
2.also instruction states that, need run "populate.py" file. before running file, think need include slug field. can specify changes in "populate.py" field?
4.solution same query, tried out answer using uuid did not work out.
you have alter migration file avoid unique constrains. according instruction, modify last migration file(which added slug field model) this:
model: slug = models.slugfield(unique=true, default=uuid.uuid1)
migration file: (i.e: 0004_category_slug.py
)
class migration(migrations.migration): dependencies = [ ('yourproject', '0003_remove_category_slug'), ] def gen_slug(apps, schema_editor): row in category.objects.all(): row.slug = slugify(row.name) row.save() operations = [ migrations.addfield( model_name='category', name='slug', field=models.slugfield(default=uuid.uuid4), preserve_default=true, ), migrations.runpython(gen_slug), migrations.alterfield( model_name='category', name='slug', field=models.slugfield(default=uuid.uuid4, unique=true), ), ]
Comments
Post a Comment