完整性错误和操作错误Django 1.6.6

Integrity Error and Operational Error Django 1.6.6

我刚开始开发一个Django博客,我正在关注这个youtube教程 - https://www.youtube.com/watch?v=7rgph8en0Jc&spfreload=1

我正在使用Django 1.6.6。

  • 每次我尝试在Admin中添加Entry时,都会收到此错误。 谢谢你的帮助!
  • Environment:

    Django Version: 1.6.6
    Python Version: 2.7.8
    Installed Applications:
    ('django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'niceblog',
    'django_markdown')
    Installed Middleware:
    ('django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware')

    1
    2
    Traceback:
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\core\handlers\base.py"

    in get_response
    112. response = wrapped_callback(request, *callback_args, >**callback_kwargs)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\contrib\admin\options.py"
    in wrapper
    450. return self.admin_site.admin_view(view)(*args, **kwargs)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\utils\decorators.py"
    in _wrapped_view
    99. response = view_func(request, *args, **kwargs)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\views\decorators\cache.py"
    in _wrapped_view_func
    52. response = view_func(request, *args, **kwargs)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\contrib\admin\sites.py"
    in inner
    198. return view(request, *args, **kwargs)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\utils\decorators.py"
    in _wrapper
    29. return bound_func(*args, **kwargs)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\utils\decorators.py"
    in _wrapped_view
    99. response = view_func(request, *args, **kwargs)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\utils\decorators.py"
    in bound_func
    25. return func(self, *args2, **kwargs2)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\transaction.py"
    in inner
    371. return func(*args, **kwargs)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\contrib\admin\options.py"
    in add_view
    1149. self.save_model(request, new_object, form, False)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\contrib\admin\options.py"
    in save_model
    878. obj.save()
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\models\base.py"
    in save
    545. force_update=force_update, update_fields=update_fields)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\models\base.py"
    in save_base
    573. updated = self._save_table(raw, cls, force_insert, force_update, using, >update_fields)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\models\base.py"
    in _save_table
    654. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\models\base.py"
    in _do_insert
    687. using=using, raw=raw)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\models\manager.py"
    in _insert
    232. return insert_query(self.model, objs, fields, **kwargs)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\models\query.py"
    in insert_query
    1514. return query.get_compiler(using=using).execute_sql(return_id)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\models\sql\compiler.py"
    in execute_sql
    903. cursor.execute(sql, params)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\backends\util.py"
    in execute
    69. return super(CursorDebugWrapper, self).execute(sql, params)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\backends\util.py"
    in execute
    53. return self.cursor.execute(sql, params)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\utils.py"
    in exit
    99. six.reraise(dj_exc_type, dj_exc_value, traceback)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\backends\util.py"
    in execute
    53. return self.cursor.execute(sql, params)
    File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\backends\sqlite3\base.py"
    in execute
    452. return Database.Cursor.execute(self, query, params)

    1
    2
    Exception Type: IntegrityError at /admin/niceblog/entry/add/
    Exception Value: niceblog_entry.slug may not be NULL
  • 每次我尝试在Admin中编辑Entry时,都会抛出此错误
  • Environment:

    Django Version: 1.6.6 Python Version: 2.7.8 Installed Applications:
    ('django.contrib.admin', 'django.contrib.auth',
    'django.contrib.contenttypes', 'django.contrib.sessions',
    'django.contrib.messages', 'django.contrib.staticfiles', 'niceblog',
    'django_markdown') Installed Middleware:
    ('django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware')

    Traceback: File
    "C:\Users\user\desktop\firstblog\lib\site-packages\django\core\handlers\base.py"
    in get_response
    112. response = wrapped_callback(request, *callback_args, **callback_kwargs) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\contrib\admin\options.py"
    in wrapper
    450. return self.admin_site.admin_view(view)(*args, **kwargs) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\utils\decorators.py"
    in _wrapped_view
    99. response = view_func(request, *args, **kwargs) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\views\decorators\cache.py"
    in _wrapped_view_func
    52. response = view_func(request, *args, **kwargs) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\contrib\admin\sites.py"
    in inner
    198. return view(request, *args, **kwargs) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\utils\decorators.py"
    in _wrapper
    29. return bound_func(*args, **kwargs) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\utils\decorators.py"
    in _wrapped_view
    99. response = view_func(request, *args, **kwargs) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\utils\decorators.py"
    in bound_func
    25. return func(self, *args2, **kwargs2) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\transaction.py"
    in inner
    371. return func(*args, **kwargs) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\contrib\admin\options.py"
    in change_view
    1255. form = ModelForm(instance=obj) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\forms\models.py"
    in init
    315. object_data = model_to_dict(instance, opts.fields, opts.exclude) File
    "C:\Users\user\desktop\firstblog\lib\site-packages\django\forms\models.py"
    in model_to_dict
    141. data[f.name] = list(f.value_from_object(instance).values_list('pk', flat=True)) File
    "C:\Users\user\desktop\firstblog\lib\site-packages\django\db\models\query.py"
    in iter
    96. self._fetch_all() File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\models\query.py"
    in _fetch_all
    857. self._result_cache = list(self.iterator()) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\models\query.py"
    in iterator
    1068. for row in self.query.get_compiler(self.db).results_iter(): File
    "C:\Users\user\desktop\firstblog\lib\site-packages\django\db\models\sql\compiler.py"
    in results_iter
    713. for rows in self.execute_sql(MULTI): File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\models\sql\compiler.py"
    in execute_sql
    786. cursor.execute(sql, params) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\backends\util.py"
    in execute
    69. return super(CursorDebugWrapper, self).execute(sql, params) File
    "C:\Users\user\desktop\firstblog\lib\site-packages\django\db\backends\util.py"
    in execute
    53. return self.cursor.execute(sql, params) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\utils.py"
    in exit
    99. six.reraise(dj_exc_type, dj_exc_value, traceback) File
    "C:\Users\user\desktop\firstblog\lib\site-packages\django\db\backends\util.py"
    in execute
    53. return self.cursor.execute(sql, params) File"C:\Users\user\desktop\firstblog\lib\site-packages\django\db\backends\sqlite3\base.py"
    in execute
    452. return Database.Cursor.execute(self, query, params)

    Exception Type: OperationalError at /admin/niceblog/entry/2/ Exception
    Value: no such table: niceblog_entry_tags

    models.py文件--->

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    class Tag(models.Model):
        slug = models.SlugField(max_length=200, unique=True)

    def __unicode__(self):
        return self.slug

    class Entry(models.Model):
        title = models.CharField(max_length=200)
        body = models.TextField()
        publish = models.BooleanField(default=True)
        created = models.DateTimeField(auto_now_add=True)
        modified = models.DateTimeField(auto_now=True)
        tags = models.ManyToManyField(Tag)

        def __unicode__(self):
            return self.title

        class Meta:
            verbose_name ="Blog Entry"
            verbose_name_plural ="Blog Entries"
            ordering = ['-created']

    admin.py文件--->

    1
    2
    3
    4
    5
    class EntryAdmin(MarkdownModelAdmin):
        list_display = ("title","created")

    admin.site.register(Entry, EntryAdmin)
    admin.site.register(Tag)


    我想,在创建表之后添加了Entry.tags。
    如果可以将django升级到1.7,那么您可以使用内置迁移。
    Firstable将您的应用重置为实际的数据库架构,即删除标签字段,然后:

    1
    2
    3
    4
    5
    6
    7
    # Create initial migrations
    $ python manage.py makemigrations your_app
    # Migrate
    $ python manage.py migrate your_app
    # Add your tags field. Repeat
    $ python manage.py makemigrations your_app
    $ python manage.py migrate your_app

    如果你想留在django 1.6,请使用South。 它几乎是一样的:

    1
    2
    3
    4
    5
    6
    7
    # Create initial migrations
    $ python manage.py schemamigration your_app intial --init
    # Migrate
    $ python manage.py migrate your_app --fake
    # Add your tags field. Create migration
    $ python manage.py schemamigration your_app add_tags --auto
    $ python manage.py migrate your_app