[Fixed]-Django: How to create object which has ManyToManyField in shell?

1👍

You need to add subject category to your product.

So do it like this:

# place category
self.place_category = PlaceCategory.objects.create(name="학교")

# subject category
self.subject_category1 = SubjectCategory.objects.create(name="사람")
self.subject_category2 = SubjectCategory.objects.create(name="꽃병")

# product
self.product = Product.objects.create(
    name="product name1",
    place_category=self.place_category,
)

self.product.subject_category_set.add(self.subject_category1)
self.product.subject_category_set.add(self.subject_category2)

or you can make something like this

list_of_subject_categories = [self.subject_category1, self.subject_category2]

self.product.subject_category_set.add(*list_of_subject_categories)
👤sebb

Leave a comment