How to get the ID of a just created record in Django?

DjangoDjango Models

Django Problem Overview


I'm using Django 1.3 for one of my projects and I need to get the ID of a record just saved in the database.

I have something like the code below to save a record in the database:

n = MyData.objects.create(record_title=title, record_content=content)
n.save()

The ID of the record just saved auto-increments. Is there a way to get that ID and use it somewhere else in my code?

Django Solutions


Solution 1 - Django

Use n.id after the save.

See "Auto-incrementing primary keys".

Solution 2 - Django

It would be n.pk.

To quote "Model.pk":

> Regardless of whether you define a > primary key field yourself, or let > Django supply one for you, each model > will have a property called pk. It > behaves like a normal attribute on the > model, but is actually an alias for > whichever attribute is the primary key > field for the model. You can read and > set this value, just as you would for > any other attribute, and it will > update the correct field in the model.

Solution 3 - Django

The ID will be automatically updated in your model, so immediately after your n.save() line you can read n.id and it will be populated.

Solution 4 - Django

Remove save() and get pk directly:

n = MyData.objects.create(record_title=title, record_content=content)
n.pk

Solution 5 - Django

If someone reading this question and after check the other answers still having problems accessing the id after the creation of the object. Be sure you don't define id as an Integer in your model. If you decide to declare it anyways, use Autofield but you don't need to, It is for free with models.Model

#No
class TestModel(models.Model):
    id = models.IntegerField(primary_key=True)
    something...


#Ok
class TestModel(models.Model):
    id = models.AutoField(primary_key=True)
    something...

#Ok
class TestModel(models.Model):
    something...

if you do define id as Integer, TestModel.objects.create( or with save() will return None.

Solution 6 - Django

I had a similar issue with accessing the id. In Django 3.0.5, this is how I accessed the id. Using your example and variable name, see below:

instance = n.save()
# return the id
instance[0].id 

The variable 'instance' above is a list. Accessing id in the methods described above returns an AttributeError ("object has no attribute 'id'") in Django 3.

This answer applies when using modelformset_factory. This is true when creating a Form class from a Django model as described in the Django docs

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionavatarView Question on Stackoverflow
Solution 1 - DjangoJamesOView Answer on Stackoverflow
Solution 2 - DjangoDavor LucicView Answer on Stackoverflow
Solution 3 - DjangoAndrew WilkinsonView Answer on Stackoverflow
Solution 4 - DjangoYevhen DyachenkoView Answer on Stackoverflow
Solution 5 - DjangoRaúl MartínView Answer on Stackoverflow
Solution 6 - DjangobucephalusView Answer on Stackoverflow