异常类型:OperationalError - 异常值:没有这样的列:blog_article.slug

时间:2016-06-25 18:39:30

标签: python django database-migration slug

我正在使用django制作博客。我创建了一个名为blog的应用程序,在 models.py 中,我为此博客上的文章创建了以下模型:

class Article(models.Model):
    title = models.CharField(max_length = 100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    author = models.CharField(max_length = 100)
    COUNTRIES = (
        ("fr", "france"),
        ("ge", "germany"),
        )
    nationality = models.CharField(choices= COUNTRIES, max_length = 20)
    publication_date = models.DateTimeField()
    content = models.TextField()

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('view_article', None, { 'slug': self.slug })

admin.py 中,我执行了以下操作,以便预先填充标题:

class ArticleAdmin(admin.ModelAdmin):
    exclude = ['posted']
    prepopulated_fields = {'slug': ('title',)}

我已经多次运行 python manage.py makemigrations migrate ,尝试压缩迁移,就像我在其他帖子中找到的那样。当我运行 makemigrations 时,我得到:“未检测到任何更改”。当我运行 migrate 时,我收到“No migration to apply”。

但是,当我 runserver 并转到管理界面以添加新文章时,请填写“标题”字段,查看自动填充的slug字段(由于预填充)并希望 SAVE ,这是我得到的:

Request Method: POST
Request URL: http://127.0.0.1:8000/admin/blog/article/add/

Django Version: 1.9.4
Python Version: 2.7.11
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'blog.apps.BlogConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
  541.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
  244.             return view(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in add_view
  1437.         return self.changeform_view(request, None, form_url, extra_context)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in inner
  184.                     return func(*args, **kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in changeform_view
  1370.             if form.is_valid():

File "/Library/Python/2.7/site-packages/django/forms/forms.py" in is_valid
  161.         return self.is_bound and not self.errors

File "/Library/Python/2.7/site-packages/django/forms/forms.py" in errors
  153.             self.full_clean()

File "/Library/Python/2.7/site-packages/django/forms/forms.py" in full_clean
  364.         self._post_clean()

File "/Library/Python/2.7/site-packages/django/forms/models.py" in _post_clean
  402.             self.validate_unique()

File "/Library/Python/2.7/site-packages/django/forms/models.py" in validate_unique
  411.             self.instance.validate_unique(exclude=exclude)

File "/Library/Python/2.7/site-packages/django/db/models/base.py" in validate_unique
  922.         errors = self._perform_unique_checks(unique_checks)

File "/Library/Python/2.7/site-packages/django/db/models/base.py" in _perform_unique_checks
  1017.             if qs.exists():

File "/Library/Python/2.7/site-packages/django/db/models/query.py" in exists
  651.             return self.query.has_results(using=self.db)

File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in has_results
  501.         return compiler.has_results()

File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in has_results
  819.         return bool(self.execute_sql(SINGLE))

File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  848.             cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/utils.py" in __exit__
  95.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
  323.         return Database.Cursor.execute(self, query, params)

**Exception Type: OperationalError at /admin/blog/article/add/
Exception Value: no such column: blog_article.slug**

如果我点击管理界面中的“文章”,我会得到相同的结果。我试图压缩迁移,制作迁移并再次迁移,但它没有用......我到处寻找并没有找到对我有用的答案 - 非常感谢你,如果你能给出一些提示!

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法吗?

检查您的迁移文件夹。我们假设您已在my_project/blog/migrations/0020_...下添加了文章,那么您可以尝试运行:

./manage.py migrate blog 0019 =>这应该会给你一个错误(blog_article表不存在 - 无法删除)

然后伪造它: ./manage.py migrate blog 0019 --fake

然后再次迁移: ./manage.py migrate blog