Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the last inserted id in django

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

like image 652
user1746291 Avatar asked Feb 12 '13 12:02

user1746291


1 Answers

You can get latest create obj like this:

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

more info here

like image 61
UnLiMiTeD Avatar answered Sep 21 '22 07:09

UnLiMiTeD