2015-02-07 207 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將作出獨特的蛞蝓