Get the last inserted id in django


Question

I am migrating some data from other databases , so i am using raw sql queries for inserting data into database . But i don't know how to get last inserted id from raw sql queries in django. I have tried this

affected_count1=cursor2.execute("table')")

and

SELECT IDENT_CURRENT(‘MyTable’)

but it gives me the error of "(1305, 'FUNCTION pydev.SCOPE_IDENTITY does not exist')"

So please tell me how can i get the last inserted id in raw sq l queries in django

1
7
2/12/2013 12:01:58 PM

You can get latest create obj like this:

obj = Foo.objects.latest('id')

more info here

20
2/12/2013 12:38:10 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon