如何获取使用 django bulk_create 创建的对象的主键 [英] How to get primary keys of objects created using django bulk_create

查看:34
本文介绍了如何获取使用 django bulk_create 创建的对象的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获取您在 django 1.4+ 中使用 bulk_create 功能创建的项目的主键?

解决方案

2016

自 Django 1.10 - 现在支持(仅在 Postgres 上)这里是 链接到文档.

<预><代码>>>>list_of_objects = Entry.objects.bulk_create([... Entry(headline="Django 2.0 发布"),... Entry(headline="Django 2.1 宣布"),... Entry(headline="Breaking: Django is awesome")...])>>>list_of_objects[0].id1

来自更改日志:

<块引用>

在 Django 1.10 中更改:添加了在使用 PostgreSQL 时对使用 bulk_create() 创建的对象设置主键的支持

Is there a way to get the primary keys of the items you have created using the bulk_create feature in django 1.4+?

解决方案

2016

Since Django 1.10 - it's now supported (on Postgres only) here is a link to the doc.

>>> list_of_objects = Entry.objects.bulk_create([
...     Entry(headline="Django 2.0 Released"),
...     Entry(headline="Django 2.1 Announced"),
...     Entry(headline="Breaking: Django is awesome")
... ])
>>> list_of_objects[0].id
1

From the change log:

Changed in Django 1.10: Support for setting primary keys on objects created using bulk_create() when using PostgreSQL was added

这篇关于如何获取使用 django bulk_create 创建的对象的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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