开发者

Django Model copy with ManyToManyField

开发者 https://www.devze.com 2023-01-10 15:21 出处:网络
Trying to copy a Django model with ManyToManyField. the model is class Book(models.Model): cats = models.ManyToMa开发者_JAVA技巧nyField(Category)

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

精彩评论

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