使用ManyToManyField的Django模型副本

时间:2010-08-09 11:56:02

标签: django

尝试使用ManyToManyField复制Django模型。

模型是

class Book(models.Model):
    cats = models.ManyToManyField(Category)

观点:

for book in books:
    book.name = "New Name"
    messageinfo = message.save()

    msg = Book(title=book.title, subject=book.subject)
    msg.save()

直到这里的作品,制作一本书

以下内容出现错误

    for cat in book.cats:
            info = Category.objects.get(id=cat.id)
            msg.cats.add(info)

产生错误

    TypeError at /
    'ManyRelatedManager' object is not iterable

1 个答案:

答案 0 :(得分:1)

book.cats.all()

Example usagedocs