每次在Django对象上调用save()
方法时,Django都会执行两个查询INSERT
和一个SELECT
。在我的情况下,这是有用的,除了一些特定的地方,每个查询是昂贵的。有关如何有时声明不需要返回任何对象的任何想法 - 不需要SELECT
。Django - 插入不返回已保存对象的ID
此外,我正在使用django-mssql来连接,这个问题似乎并不存在于MySQL上。
编辑:一个更好的解释
h = Human()
h.name='John Foo'
print h.id # Returns None, No insert has been done therefore no id is available
h.save()
print h.id # Returns the ID, an insert has taken place and also a select statement to return the id
有时候我不需求的retruning ID,只需插入
嗯,这很奇怪,MSSQL支持['INSERT ... OUTPUT'](http://msdn.microsoft.com/en-us/library/ms177564.aspx),所以我不明白为什么它会问题两个查询,但后来我再也不熟悉'django-mssql'。也许你可以为昂贵的查询构建自己的INSERT语句? – 2011-06-07 10:35:04