python - 在django中获取最后插入的id

标签 python django rawsql

我正在从其他数据库迁移一些数据,所以我使用原始 sql 查询将数据插入数据库。但我不知道如何从 django 中的原始 sql 查询中获取最后插入的 id。这个我试过了

affected_count1=cursor2.execute("table')")

and

SELECT IDENT_CURRENT(‘MyTable’)

但它给我的错误是"(1305, 'FUNCTION pydev.SCOPE_IDENTITY does not exist')"

所以请告诉我如何在 django 中获取原始 sql 查询中最后插入的 id

最佳答案

您可以像这样获取最新的创建对象:

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

更多信息 here

关于python - 在django中获取最后插入的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14832115/

相关文章:

python - on_mouse_down 调用每个按钮(甚至滚轮移动)

python - 使用 cloudformation 创建时 sns 无法触发 lambda

python - tf.Data : what are stragglers in parallel interleaving?

python - Django 老问题 : Get current user in model

python - Django错误 "no such column: tagging_tag.name"

python - Django-Tinymce导入报错

mysql - Django:使用 LEFT JOIN 聚合原始 SQL,无外键,并注入(inject)到 DetailView(上下文)

python - Pandas 组合多个 csv 文件