我有以下模特:
class Category(models.Model):
title = models.CharField(max_length=200)
class Page(models.Model):
title = models.CharField(max_length=255)
category = models.ForeignKey(Category)
class PageExt(Page):
is_t = models.BooleanField()
一些' Page'已存在于数据库中。我需要创建' PageExt'某些'类别'来自存在' Page'。如何使用ORM和优化如bulk_create?
答案 0 :(得分:0)
这个怎么样:
#categories are the objects you know you want to make PageExt for
pages = Page.objects.filter(category__in = categories)
page_exts = []
for page in pages:
#Give some values for is_t since, null is not allowed assuming True for now
page_exts.append(PageExt(is_t = True))
PageExt.objects.bulk_create(page_exts)