如何获取Django中刚刚创建的记录的ID? [英] How to get the ID of a just created record in Django?

查看:380
本文介绍了如何获取Django中刚刚创建的记录的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的一个项目使用Django 1.3,我需要获取刚刚保存在数据库中的记录的ID.

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()

记录的ID刚刚保存了自动增量.有没有办法获取该ID并在我的代码中的其他地方使用它?

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?

推荐答案

保存后使用 n.id .

请参阅"自动递增主键".

这篇关于如何获取Django中刚刚创建的记录的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆