简体   繁体   English

在 Django 中批量创建相关模型

[英]Bulk creating related models in Django

Consider two simple related models:考虑两个简单的相关模型:

class A(models.Model):
    id = models.IntegerField(primary_key=True)

class B(models.Model):
    a = models.ForeignKey(A)
    # other fields

Before doing a very large bulk insertion on B :在对B进行非常大的批量插入之前:

lots_of_b_objects = [B(a_id=1234), B(a_id=5678), ...] 
B.objects.bulk_create(lots_of_b_objects)

(Note that for performance reasons I never actually hold A objects in the bulk creation, I only refer to their well-known id, whether it exists or not) (请注意,出于性能原因,我从未在批量创建中实际持有A对象,我仅参考其众所周知的 id,无论是否存在)

What's a highly performant way to ensure all the related A objects also exist?确保所有相关A对象也存在的高性能方法是什么?

Right now the best solution I have is to predetermine the set of related A 's and run get_or_create() for each.现在我最好的解决方案是预先确定一组相关的A并为每个运行get_or_create() This isn't fast enough.这还不够快。 Is there a better way to create all the A objects before doing the bulk insert?在进行批量插入之前,有没有更好的方法来创建所有A对象?

De-normalizing the models is not an option here, since the data model is slightly more complicated that described.对模型进行反规范化不是一种选择,因为数据模型比所描述的要稍微复杂一些。

It's a hackish way but something like this should be far better than using get_or_create in a loop (But it may vary case to case, so I don't know this way can be valid for you or not).这是一种骇人听闻的方式,但像这样的方式应该比在循环中使用get_or_create (但它可能因情况而异,所以我不知道这种方式对您是否有效)。

existing_As = A.objects.filter(id__in=a_ids).values_list('id', flat=True)
As_to_create = list(set(a_ids) - set(existing_As))
A.objects.bulk_create([A(id=x) for x in As_to_create])

# Now we are sure all the As exist as we just created them, so
lots_of_b_objects = [B(a_id=1234), B(a_id=5678), ...] 
B.objects.bulk_create(lots_of_b_objects)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM