Django在执行bulk_create时设置many_to_many对象 [英] Django setting many_to_many object while doing a bulk_create

查看:158
本文介绍了Django在执行bulk_create时设置many_to_many对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.9,并正在尝试bulk_create创建许多新的模型对象,并将它们与一个公共的相关many_to_many对象相关联.

I am using Django 1.9 and am trying the bulk_create to create many new model objects and associate them with a common related many_to_many object.

我的模型如下

#Computational Job object
class OT_job(models.Model):
    is_complete = models.BooleanField()
    is_submitted = models.BooleanField()
    user_email = models.EmailField()

#Many sequences
class Seq(models.Model):
    sequence=models.CharField(max_length=100)
    ot_job = models.ManyToManyField(OT_job)

我有成千上万个Seq对象,这些对象已提交并且必须与其关联的工作相关联.以前,我使用迭代器,并将其保存在for循环中.但是看完之后才意识到Django 1.9具有bulk_create.

I have thousands of Seq objects that are submitted and have to be associated with their associated job. Previously I was using an iterator and saving them in a for loop. But after reading realized that Django 1.9 has bulk_create.

我现在正在做

DNASeqs_list = [] 
for a_seq in some_iterable_with_my_data:
    # I create new model instances and add them to the list
    DNASeqs_list.append(Seq(sequence=..., ))

我现在要批量创建这些序列,并将其与current_job_object关联.

I now want to bulk_create these sequence and associate them with the current_job_object.

created_dnaseqs = Seq.objects.bulk_create(DNASeqs_list)
# How do I streamline this part below
for a_seq in created_dnaseqs:
    # Had to call save here otherwise got an error
    a_seq.save()
    a_seq.ot_job.add(curr_job_obj)

我必须在for循环中调用"a_seq.save()",因为我在执行"a_seq.ot_job.add(curr_job_obj)"的部分出现错误

I had to call "a_seq.save()" in for loop because I got an error in the part where I was doing "a_seq.ot_job.add(curr_job_obj)" which said

....需要使用字段"seq"的值,然后才能使用多对多关系.

....needs to have a value for field "seq" before this many-to-many relationship can be used.

尽管阅读有关此主题的其他问题,我仍然感到困惑,因为与其他人不同,我没有定制的直通"模型.我对如何最好地将OT_Job与许多Seq关联在一起而对数据库的命中最少感到困惑.

Despite reading the other questions on this topic , I am still confused because unlike others I do not have a custom "through" model. I am confused with how best to associate the OT_Job with many Seqs with minimal hits to database.

推荐答案

从文档 https://docs.djangoproject.com/en/1.9/ref/models/querysets/#bulk-create :

如果模型的主键是自动字段",则它不会像save()一样检索和设置主键属性.

If the model’s primary key is an AutoField it does not retrieve and set the primary key attribute, as save() does.

它不适用于多对多关系.

It does not work with many-to-many relationships.

bulk_create只会创建对象,而不会像save那样将PK检索到变量中.您必须重新查询数据库以获取新创建的对象,然后创建M2M关系,但这听起来不合适,并且当前的方法是当前的最佳解决方案.

bulk_create literally will just create the objects, it does not retrieve the PK into the variable as save does. You would have to re-query the db to get your newly created objects, and then create the M2M relationships, but it sounds like that would not be appropriate and that your current method is currently the best solution.

这篇关于Django在执行bulk_create时设置many_to_many对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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