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

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

问题描述

解决方案

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

Django 1.10 - 现在支持(仅限Postgres!)这里是一个链接到文档

 >>> list_of_objects = Entry.objects.bulk_create([
... Entry(headline =Django 1.0 Released),
... Entry(headline =Django 1.1 Announced),
。 ..条目(标题=破碎:Django真棒)
...])
>>> list_of_objects [0] .id
1

从更改日志中:


在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

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 1.0 Released"),
...     Entry(headline="Django 1.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天全站免登陆