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?
You can use exists() : if scorm. objects. filter(Header__id=qp.id).
Django will create or use an autoincrement column named id by default, which is the same as your legacy column.
By default, Django adds an id field to each model, which is used as the primary key for that model. You can create your own primary key field by adding the keyword arg primary_key=True to a field. If you add your own primary key field, the automatic one will not be added.
It doesn't matter. pk is more independent from the actual primary key field i.e. you don't need to care whether the primary key field is called id or object_id or whatever. It also provides more consistency if you have models with different primary key fields.
Use n.id
after the save.
See "Auto-incrementing primary keys".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With