0

我有以下型号:

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()

某些“页面”已存在于数据库中。我需要从现有的“页面”中为某些“类别”创建“PageExt”。使用 ORM 和像 bulk_create 这样的优化如何正确?

4

1 回答 1

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)
于 2012-10-23T21:27:31.800 回答