[Answered ]-How to handle ManyToMany fields while testing django-models?

1๐Ÿ‘

โœ…

.set() expects an iterable as argument, but the argument that you pass, self.test_category, is not iterable.
You have two options:

  • Use .set([self.test_category,]) instead of .set(self.test_category). Thereby you are passing a list, which is iterable, instead of a single object, which is not iterable (therefor the error message that you get).
  • Alternatively, you could use .add(self.test_category). The function .add() takes individual objects as positional arguments, and adds them to any pre-existing catetories.

You should probably opt for .set() though, because this ensures that after its execution, the set of associated categories will be precisely the one that you pass.

Leave a comment