Direct assignment to the reverse side of a many-to-many set is prohibited. Use products.set() instead. Django...
up vote
0
down vote
favorite
I get the following error when I POST a new product with a category (the product information is formatted like this:)
{
"product_code": "testcode",
"name": "testname",
"quantity": 22,
"price": 22,
"categories": [{
"name": "Test category",
"products": ,
"categories":
}]
}
the error
Direct assignment to the reverse side of a many-to-many set is prohibited. Use products.set() instead.
and when I use
product.categories.set(**category)
I get the following error
set() got an unexpected keyword argument 'name'
My models.py file
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=255)
categoriesId = models.ForeignKey('self', related_name='categories',on_delete=models.CASCADE, blank=True, null=True)
class Product(models.Model):
product_code = models.CharField(max_length=255)
name = models.CharField(max_length=255)
price = models.IntegerField()
quantity = models.IntegerField()
categories = models.ManyToManyField(Category, related_name='products')
my serializers.py file
from rest_framework import serializers
from products_and_categories.models import Product, Category
from django.db import models
class CategorySerializer(serializers.ModelSerializer):
def to_representation(self, obj):
if 'categories' not in self.fields:
self.fields['categories'] = CategorySerializer(obj, many=True)
return super(CategorySerializer, self).to_representation(obj)
class Meta:
model = Category
fields = ("name", 'products', 'categories')
class ProductSerializer(serializers.ModelSerializer):
categories = CategorySerializer(many=True)
class Meta:
model = Product
fields = ("product_code", "name", "quantity", "price", 'categories')
def create(self, validated_data):
category_data = validated_data.pop('categories')
product = Product.objects.create(**validated_data)
for category in category_data:
product.categories.create(**category)
return product
Any idea what's going on ?
python django django-rest-framework
add a comment |
up vote
0
down vote
favorite
I get the following error when I POST a new product with a category (the product information is formatted like this:)
{
"product_code": "testcode",
"name": "testname",
"quantity": 22,
"price": 22,
"categories": [{
"name": "Test category",
"products": ,
"categories":
}]
}
the error
Direct assignment to the reverse side of a many-to-many set is prohibited. Use products.set() instead.
and when I use
product.categories.set(**category)
I get the following error
set() got an unexpected keyword argument 'name'
My models.py file
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=255)
categoriesId = models.ForeignKey('self', related_name='categories',on_delete=models.CASCADE, blank=True, null=True)
class Product(models.Model):
product_code = models.CharField(max_length=255)
name = models.CharField(max_length=255)
price = models.IntegerField()
quantity = models.IntegerField()
categories = models.ManyToManyField(Category, related_name='products')
my serializers.py file
from rest_framework import serializers
from products_and_categories.models import Product, Category
from django.db import models
class CategorySerializer(serializers.ModelSerializer):
def to_representation(self, obj):
if 'categories' not in self.fields:
self.fields['categories'] = CategorySerializer(obj, many=True)
return super(CategorySerializer, self).to_representation(obj)
class Meta:
model = Category
fields = ("name", 'products', 'categories')
class ProductSerializer(serializers.ModelSerializer):
categories = CategorySerializer(many=True)
class Meta:
model = Product
fields = ("product_code", "name", "quantity", "price", 'categories')
def create(self, validated_data):
category_data = validated_data.pop('categories')
product = Product.objects.create(**validated_data)
for category in category_data:
product.categories.create(**category)
return product
Any idea what's going on ?
python django django-rest-framework
Why does your categories data itself contain keys for categories (and products)?
– Daniel Roseman
Nov 21 at 21:06
that's the way it's intended , it's supposed to be a tree like nested structure, the ability to have categories inside of categories .. and products of course .. because it needs to have products.. otherwise what's the point of the category
– Amr S.
Nov 21 at 21:21
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I get the following error when I POST a new product with a category (the product information is formatted like this:)
{
"product_code": "testcode",
"name": "testname",
"quantity": 22,
"price": 22,
"categories": [{
"name": "Test category",
"products": ,
"categories":
}]
}
the error
Direct assignment to the reverse side of a many-to-many set is prohibited. Use products.set() instead.
and when I use
product.categories.set(**category)
I get the following error
set() got an unexpected keyword argument 'name'
My models.py file
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=255)
categoriesId = models.ForeignKey('self', related_name='categories',on_delete=models.CASCADE, blank=True, null=True)
class Product(models.Model):
product_code = models.CharField(max_length=255)
name = models.CharField(max_length=255)
price = models.IntegerField()
quantity = models.IntegerField()
categories = models.ManyToManyField(Category, related_name='products')
my serializers.py file
from rest_framework import serializers
from products_and_categories.models import Product, Category
from django.db import models
class CategorySerializer(serializers.ModelSerializer):
def to_representation(self, obj):
if 'categories' not in self.fields:
self.fields['categories'] = CategorySerializer(obj, many=True)
return super(CategorySerializer, self).to_representation(obj)
class Meta:
model = Category
fields = ("name", 'products', 'categories')
class ProductSerializer(serializers.ModelSerializer):
categories = CategorySerializer(many=True)
class Meta:
model = Product
fields = ("product_code", "name", "quantity", "price", 'categories')
def create(self, validated_data):
category_data = validated_data.pop('categories')
product = Product.objects.create(**validated_data)
for category in category_data:
product.categories.create(**category)
return product
Any idea what's going on ?
python django django-rest-framework
I get the following error when I POST a new product with a category (the product information is formatted like this:)
{
"product_code": "testcode",
"name": "testname",
"quantity": 22,
"price": 22,
"categories": [{
"name": "Test category",
"products": ,
"categories":
}]
}
the error
Direct assignment to the reverse side of a many-to-many set is prohibited. Use products.set() instead.
and when I use
product.categories.set(**category)
I get the following error
set() got an unexpected keyword argument 'name'
My models.py file
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=255)
categoriesId = models.ForeignKey('self', related_name='categories',on_delete=models.CASCADE, blank=True, null=True)
class Product(models.Model):
product_code = models.CharField(max_length=255)
name = models.CharField(max_length=255)
price = models.IntegerField()
quantity = models.IntegerField()
categories = models.ManyToManyField(Category, related_name='products')
my serializers.py file
from rest_framework import serializers
from products_and_categories.models import Product, Category
from django.db import models
class CategorySerializer(serializers.ModelSerializer):
def to_representation(self, obj):
if 'categories' not in self.fields:
self.fields['categories'] = CategorySerializer(obj, many=True)
return super(CategorySerializer, self).to_representation(obj)
class Meta:
model = Category
fields = ("name", 'products', 'categories')
class ProductSerializer(serializers.ModelSerializer):
categories = CategorySerializer(many=True)
class Meta:
model = Product
fields = ("product_code", "name", "quantity", "price", 'categories')
def create(self, validated_data):
category_data = validated_data.pop('categories')
product = Product.objects.create(**validated_data)
for category in category_data:
product.categories.create(**category)
return product
Any idea what's going on ?
python django django-rest-framework
python django django-rest-framework
asked Nov 21 at 19:36
Amr S.
305
305
Why does your categories data itself contain keys for categories (and products)?
– Daniel Roseman
Nov 21 at 21:06
that's the way it's intended , it's supposed to be a tree like nested structure, the ability to have categories inside of categories .. and products of course .. because it needs to have products.. otherwise what's the point of the category
– Amr S.
Nov 21 at 21:21
add a comment |
Why does your categories data itself contain keys for categories (and products)?
– Daniel Roseman
Nov 21 at 21:06
that's the way it's intended , it's supposed to be a tree like nested structure, the ability to have categories inside of categories .. and products of course .. because it needs to have products.. otherwise what's the point of the category
– Amr S.
Nov 21 at 21:21
Why does your categories data itself contain keys for categories (and products)?
– Daniel Roseman
Nov 21 at 21:06
Why does your categories data itself contain keys for categories (and products)?
– Daniel Roseman
Nov 21 at 21:06
that's the way it's intended , it's supposed to be a tree like nested structure, the ability to have categories inside of categories .. and products of course .. because it needs to have products.. otherwise what's the point of the category
– Amr S.
Nov 21 at 21:21
that's the way it's intended , it's supposed to be a tree like nested structure, the ability to have categories inside of categories .. and products of course .. because it needs to have products.. otherwise what's the point of the category
– Amr S.
Nov 21 at 21:21
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419386%2fdirect-assignment-to-the-reverse-side-of-a-many-to-many-set-is-prohibited-use-p%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why does your categories data itself contain keys for categories (and products)?
– Daniel Roseman
Nov 21 at 21:06
that's the way it's intended , it's supposed to be a tree like nested structure, the ability to have categories inside of categories .. and products of course .. because it needs to have products.. otherwise what's the point of the category
– Amr S.
Nov 21 at 21:21