Reputation: 45
I want add this product to both category, how to do it
class Category(models.Model):
name = models.CharField(max_length=500)
image = models.ImageField(upload_to='categories', default='default.png')
class Products(models.Model):
name = models.CharField(max_length=500)
category_id = models.ManyToManyField(Category, default='')
category_id = [4, 6]
product, created = Products.objects.get_or_create(name =data['Casual Red Shirt'],
product.category_id.add()
Upvotes: 1
Views: 140
Reputation: 1649
Try this
category_id = [4, 6]
product, created = Products.objects.get_or_create(name =data['Casual Red Shirt'],
product.category_id.add(* category_id)
Upvotes: 2