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 ?










share|improve this question






















  • 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















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 ?










share|improve this question






















  • 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













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 ?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Basket-ball féminin

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

I want to find a topological embedding $f : X rightarrow Y$ and $g: Y rightarrow X$, yet $X$ is not...