0

I have following models:

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

Some 'Page' already exists in database. I need to create 'PageExt' for some 'Category' from exists 'Page'. How it right to do with using ORM and optimization like 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 に答える