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

时间:2014-10-14 06:27:17

标签: django django-models

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

我正在使用Django 1.6.6。

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

    环境:

         

    Django版本:1.6.6      Python版本:2.7.8      已安装的应用程序      (' django.contrib.admin&#39 ;,        ' django.contrib.auth&#39 ;,        ' django.contrib.contenttypes&#39 ;,        ' django.contrib.sessions&#39 ;,        ' django.contrib.messages&#39 ;,        ' django.contrib.staticfiles&#39 ;,        ' niceblog&#39 ;,        ' django_markdown')       已安装的中间件:       (' django.contrib.sessions.middleware.SessionMiddleware&#39 ;,        ' django.middleware.common.CommonMiddleware&#39 ;,        ' django.middleware.csrf.CsrfViewMiddleware&#39 ;,        ' django.contrib.auth.middleware.AuthenticationMiddleware&#39 ;,        ' django.contrib.messages.middleware.MessageMiddleware&#39 ;,        ' django.middleware.clickjacking.XFrameOptionsMiddleware&#39)

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

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

    Exception Type: IntegrityError at /admin/niceblog/entry/add/
    Exception Value: niceblog_entry.slug may not be NULL
    
    1. 每当我尝试编辑管理员中的条目时,都会抛出此错误
    2.   

      环境:

           

      Django版本:1.6.6 Python版本:2.7.8已安装的应用程序:   (' django.contrib.admin',' django.contrib.auth',   ' django.contrib.contenttypes',' django.contrib.sessions',   ' django.contrib.messages',' django.contrib.staticfiles',' niceblog',   ' django_markdown')已安装的中间件:   (' django.contrib.sessions.middleware.SessionMiddleware&#39 ;,   ' django.middleware.common.CommonMiddleware&#39 ;,   ' django.middleware.csrf.CsrfViewMiddleware&#39 ;,   ' django.contrib.auth.middleware.AuthenticationMiddleware&#39 ;,   ' django.contrib.messages.middleware.MessageMiddleware&#39 ;,   ' django.middleware.clickjacking.XFrameOptionsMiddleware&#39)

           

      回溯:文件   " C:\用户\用户\桌面\ firstblog \ lib中\站点包\ django的\芯\处理程序\ base.py"   在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"   在包装中     450. return self.admin_site.admin_view(view)(* args,** kwargs)File" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ utils \ decorators.py&#34 ;   在_wrapped_view中     99. response = view_func(request,* args,** kwargs)File" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ views \ decorators \ cache.py"   在_wrapped_view_func中     52. response = view_func(request,* args,** kwargs)File" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ contrib \ admin \ sites.py"   在内心     198.返回视图(request,* args,** kwargs)文件" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ utils \ decorators.py"   在_wrapper中     29. return bound_func(* args,** kwargs)File" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ utils \ decorators.py"   在_wrapped_view中     99. response = view_func(request,* args,** kwargs)File" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ utils \ decorators.py"   在bound_func中     25. return func(self,* args2,** kwargs2)File" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ db \ transaction.py"   在内心     371. return func(* args,** kwargs)File" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ contrib \ admin \ options.py"   在change_view中     1255. form = ModelForm(instance = obj)File" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ forms \ models.py"   在 init     315. object_data = model_to_dict(instance,opts.fields,opts.exclude)文件   " C:\用户\用户\桌面\ firstblog \ lib中\站点包\ django的\形式\ models.py"   在model_to_dict中     141. data [f.name] = list(f.value_from_object(instance).values_list(' pk',flat = True))文件   " C:\用户\用户\桌面\ firstblog \ LIB \站点包\ Django的\ DB \型号\ query.py"   在 iter     96. self._fetch_all()文件" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ db \ models \ query.py"   在_fetch_all中     857. self._result_cache = list(self.iterator())File" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ db \ models \ query.py"   在迭代器中     1068.对于self.query.get_compiler中的行(self.db).results_iter():文件   " C:\用户\用户\桌面\ firstblog \ LIB \站点包\ Django的\ DB \型号\ SQL \ compiler.py"   在results_iter中     713.对于self.execute_sql(MULTI)中的行:File" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ db \ models \ sql \ compiler.py"   在execute_sql中     786. cursor.execute(sql,params)文件" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ db \ backends \ util.py"   执行中     69. return super(CursorDebugWrapper,self).execute(sql,params)文件   " C:\用户\用户\桌面\ firstblog \ lib中\站点包\ django的\分贝\后端\ util.py"   执行中     53. return self.cursor.execute(sql,params)File" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ db \ utils.py"   在退出     99. six.reraise(dj_exc_type,dj_exc_value,traceback)文件   " C:\用户\用户\桌面\ firstblog \ lib中\站点包\ django的\分贝\后端\ util.py"   执行中     53. return self.cursor.execute(sql,params)File" C:\ Users \ user \ desktop \ firstblog \ lib \ site-packages \ django \ db \ backends \ sqlite3 \ base.py"   执行中     452.返回Database.Cursor.execute(self,query,params)

           

      异常类型:/ admin / niceblog / entry / 2 / Exception中的OperationalError   值:没有这样的表:niceblog_entry_tags

      models.py文件--->

      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文件--->

      class EntryAdmin(MarkdownModelAdmin):
          list_display = ("title", "created")
      
      admin.site.register(Entry, EntryAdmin) 
      admin.site.register(Tag)
      

1 个答案:

答案 0 :(得分:1)

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

# 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。它几乎一样:

# 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