开发者

Django - Insert Without Returning the Id of the Saved Object

Each time the save() method is called on a Django object, Django executes t开发者_如何学运维wo queries one INSERT and one SELECT. In my case this is usefull except for some specific places where each query is expensive. Any ideas on how to sometimes state that no object needs to be returned - no SELECT needed.

Also I'm using django-mssql to connect to, this problem doesn't seem to exist on MySQL.

EDIT : A better explanation

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

Sometimes I don't the need the retruning ID, just insert


40ins's answer was right, but probably it might have higher costs...

When django execustes a save(), it needed to be sure if the object is a new one or an existing one. So it hits the database to check if related objext exists. If yes, it executes an UPDATE, orherwise it executes an ISERT

Check documentatin from here...

You can use force_insert or force_update ,but that might be cause serious data integrity problems, like creating a duplicate entry instead of updating the existing one...

So, if you wish to use force , you must be sure whether it will be an INSERT or an UPDATE...


Try to use save() method with force_insert or force_update attributes. With this attributes django knows about record existence and don't make additional query.


The additional select is the django-mssql backend getting the identity value from the table to determine the ID that was just inserted. If this select is slow, then something is wrong with your SQL server/configuration because it is only doing SELECT CAST(IDENT_CURRENT(*table_name*) as bigint) call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜