2015-02-07 177 views
0

我一直在阅读tango_with_django教程,当我设法进入第7部分时,我被困住了。Django迁移错误

当我加入这个代码

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 

之后我删除了这段代码我有了这个

Traceback (most recent call last): File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line 
    utility.execute() File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 377, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Python34\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv 
    self.execute(*args, **options.__dict__) File "C:\Python34\lib\site-packages\django\core\management\base.py", line 338, in execute 
    output = self.handle(*args, **options) File "C:\Python34\lib\site-packages\django\core\management\commands\migrate.py ", line 161, in handle 
    executor.migrate(targets, plan, fake=options.get("fake", False)) File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 68 , in migrate 
    self.apply_migration(migration, fake=fake) File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 10 2, in apply_migration 
    migration.apply(project_state, schema_editor) File "C:\Python34\lib\site-packages\django\db\migrations\migration.py", line 1 08, in apply 
    operation.database_forwards(self.app_label, schema_editor, project_state, ne w_state) File "C:\Python34\lib\site-packages\django\db\migrations\operations\fields.py" , line 37, in database_forwards 
    field, File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\schema.py", lin e 176, in add_field 
    self._remake_table(model, create_fields=[field]) File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\schema.py", lin e 144, in _remake_table 
    self.quote_name(model._meta.db_table), File "C:\Python34\lib\site-packages\django\db\backends\schema.py", line 102, i n execute 
    cursor.execute(sql, params) File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 81, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 65, in execute 
    return self.cursor.execute(sql, params) File "C:\Python34\lib\site-packages\django\db\utils.py", line 94, in 
__exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) File "C:\Python34\lib\site-packages\django\utils\six.py", line 658, in reraise 

    raise value.with_traceback(tb) File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 65, in execute 
    return self.cursor.execute(sql, params) File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\base.py", line 485, in execute 
    return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError: UNIQUE constraint failed: rango_category__new.sl ug 

,我仍然得到同样的错误信息,同时试图做

python manage.py migrate 

我删除了数据库后

python manage.py flush 

,进入

python manage.py migrate 

我也得到了同样的错误。

发生了什么事?

+0

方面做很多的辛勤工作为你你尝试过'python manage.py syncdb'吗?另外,尝试从你的'SlugField'中删除'unique = True'。 – 2015-02-07 13:25:49

+0

它不会工作。相同的错误消息 – starwars25 2015-02-07 19:08:55

回答

0

尝试回滚迁移到以前的状态。假设这是您第一次迁移 - 让回滚到零

python manage.py migrate category zero 

塞逻辑可以得到一个有点复杂 尽管名称是不同的,他们可以有相同的蛞蝓 例如 名称:富酒吧 弹头:FOO吧

名称:富酒吧 弹头:FOO吧

名称:富酒吧 弹头:FOO吧

名称:富!酒吧 弹头:FOO吧

...你的想法

你跃跃欲试想要独特蛞蝓使URL访问不错 例如domain.com/category/1234(使用id)的 VS domain.com/category/foo-bar

看看AutoSlugField将作出独特的蛞蝓