Get last inserted id in django

I am transferring some data from other databases, so I use raw SQL queries to insert data into the database. But I don't know how to get the last inserted id from raw sql queries in django. I tried this

affected_count1=cursor2.execute("table')") and SELECT IDENT_CURRENT('MyTable') 

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

So please tell me how can I get the last inserted id in raw sql queries in django

+4
source share
2 answers

You can get the latest obj creation as follows:

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

more details here

+15
source

In Django 1.6

 obj = Foo.objects.latest('id') obj = Foo.objects.earliest('id') 
+3
source

All Articles