Django – ManyToManyField 에 리스트 값 할당

아래와 같은 ManyToManyField 필드에 값을 대입하고 싶을때 그냥,

products = models.ManyToManyField(Product, blank=True)

item.products = body.products

이런식으로 했더니, 아래와 같이 에러 발생

The direct assignment to the forward side of a many-to-many set is not allowed. Instead, it is recommended to use the products.set() method for assigning values.

즉, 해결하려면 아래와 같은 구문을 사용해야 함.

item.products.set(body.products)

https://stackoverflow.com/questions/50015204/direct-assignment-to-the-forward-side-of-a-many-to-many-set-is-prohibited-use-e

Share: Twitter Facebook
Bongjun Hur's Picture

About Bongjun Hur

BJ is a dev.

Seoul, Korea https://devbj.com