每次在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