1

Trying to copy a Django model with ManyToManyField.

the model is

class Book(models.Model):
    cats = models.ManyToManyField(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
bobsr
  • 3,879
  • 8
  • 30
  • 37

1 Answers1

1
book.cats.all()

Example usage and docs.

Davor Lucic
  • 28,970
  • 8
  • 66
  • 76