开发者

Django Model copy with ManyToManyField

Trying to copy a Django model with ManyToManyField.

the model is

class Book(models.Model):
    cats = models.ManyToMa开发者_JAVA技巧nyField(Category)

the view:

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

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

sort of works till here, makes a copy of book

the following gets an error

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

Error it produces

    TypeError at /
    'ManyRelatedManager' object is not iterable


book.cats.all()

Example usage and docs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜