[Django]-Access primary key/id value during creation of a Django record

3👍

Well there is no way you can access the ID before saving it.
ID will be generated by database so you have to call save method so django sends the data to database and then you’ll have your ID

This has been explained in django documentations too:

Auto-incrementing primary keys

If you really need to access the ID which is a unique identifier, you can create one manually.

You can add a field called my_id and then give it a unique value before saving the data (you just need to check if that value already exist or not and if it does, then just create another one). This way you’ll be able to get a unique ID which you’ll be able to find that row with in future.

Leave a comment