django – 为什么我不能在编辑后保存我的模型实例?

前端之家收集整理的这篇文章主要介绍了django – 为什么我不能在编辑后保存我的模型实例?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个模型,我可以很好地实例化,但一旦创建,如果我试图保存它我得到一个IntegrityError说主键必须是唯一的.是什么导致了这个?

还有其他模型继承自Node,他们给了我同样的问题.

这是我的模特:

class Node(MPTTModel):
    title = models.CharField(max_length=255)
    parent = models.ForeignKey('self',null=True,blank=True,related_name='children')

class Book(Node):
    isbn13 = models.CharField(max_length=14)
    description = models.TextField()

这是它生成sql

CREATE TABLE "main_node" (
    "id" integer NOT NULL PRIMARY KEY,"title" varchar(255) NOT NULL,"parent_id" integer,"type" varchar(1) NOT NULL,"lft" integer unsigned NOT NULL,"rght" integer unsigned NOT NULL,"tree_id" integer unsigned NOT NULL,"level" integer unsigned NOT NULL
)
;
CREATE TABLE "main_book" (
    "node_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "main_node" ("id"),"isbn13" varchar(14) NOT NULL,"description" text NOT NULL
)
;

当我尝试保存预先存在的模型实例时会发生什么:

>>> b = Book.objects.create(title='c+ for dummies',isbn13='111-1111111111',description='nt')
>>> b.title='c++ for dummies'
>>> b.save()
Traceback (most recent call last):
  File "<console>",line 1,in <module>
  File "C:\Users\Jackie\Desktop\Code\ProjectName\main\models.py",line 56,in save
    super(Book,self).save(self,*args)
  File "C:\Python27\lib\site-packages\django\db\models\base.py",line 460,in save
    self.save_base(using=using,force_insert=force_insert,force_update=force_update)
  File "C:\Python27\lib\site-packages\django\db\models\base.py",line 553,in save_base
    result = manager._insert(values,return_id=update_pk,using=using)
  File "C:\Python27\lib\site-packages\django\db\models\manager.py",line 195,in _insert
    return insert_query(self.model,values,**kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py",line 1436,in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py",line 791,in execute_sql
    cursor = super(sqlInsertCompiler,self).execute_sql(None)
  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py",line 735,in execute_sql
    cursor.execute(sql,params)
  File "C:\Python27\lib\site-packages\django\db\backends\util.py",line 34,in execute
    return self.cursor.execute(sql,params)
  File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py",line 234,in execute
    return Database.Cursor.execute(self,query,params)
IntegrityError: PRIMARY KEY must be unique

以下是运行的查询

>>> for q in connection.queries:
...   print q['sql'] + "\n\n"
...
SELECT MAX("main_node"."tree_id") AS "tree_id__max" FROM "main_node"


INSERT INTO "main_node" ("title","parent_id","type","lft","rght","tree_id","level") VALUES (c+
 for dummies,None,B,1,2,0)


INSERT INTO "main_book" ("node_ptr_id","isbn13","description") VALUES (1,111-1111111111,nt)




SELECT (1) AS "a" FROM "main_node" WHERE "main_node"."id" = 1  LIMIT 1


UPDATE "main_node" SET "title" = c++ for dummies,"parent_id" = NULL,"type" = B,"lft" = 1,"rght"
= 2,"tree_id" = 1,"level" = 0 WHERE "main_node"."id" = 1


INSERT INTO "main_book" ("node_ptr_id",nt)

当我重新保存已创建的实例时,为什么要尝试插入main_book?

解决方法

你是否在你的任何模特中超越了“save()”方法?我有一个类似的问题,经过大量的挖掘,发现这是因为我过度使用的“保存()”中的一个小错误.

你能发布一下你的模型定义吗? (从生成sql中,看起来你发布的内容不完整)

猜你在找的Python相关文章