Here I want to send products values to the backend Django as Django have multi Serializers
But i think i have sent proper formatted value data also i am getting error Please help me.
class ProductSerializer(serializers.ModelSerializer):
product_size = ProductSizeSerializer(many = True)
product_color_stock = ProductSizeSerializer(many = True)
product_image = ProductSizeSerializer(many = True)
class Meta:
model = Product
fields = ["user","category","Slug","Name","size_Description","color_Description","Description","Main_Image","Banner","Rating","Date","archive","product_color_stock","product_size","product_image","secret_key"]
extra_kwargs = {
'user': {'read_only': True},
'Slug': {'read_only': True},
'Rating': {'read_only': True},
'Date': {'read_only': True},
'product_color_stock': {'read_only': True},
'product_image': {'read_only': True},
'product_size': {'read_only': True},
'secret_key':{'read_only': True},
}
This is the Submit function that is sent from next js to django
const handleSubmit = async (event) => {
event.preventDefault();
const products = new FormData();
const times = 1;
const secret_details = "details" + times ;
const secret_size = "size" + times ;
const secret_color_stock = "color_stock" + times ;
const secret_img = "image" + times ;
products.append("secret_key", secret_details);
products.append("user", parseInt(2));
Object.keys(p_details).forEach((key) => products.append(key, p_details[key]));
var parentObj = {};
for (var key in p_imgs.Image) {
if (p_imgs.Image.hasOwnProperty(key)) {
var file = p_imgs.Image[key];
var nameWithKey = p_imgs.Name + "[" + key + "]";
var obj = {
"Name": nameWithKey,
"user": 2,
"image": file,
"secret_key": secret_img
};
parentObj[nameWithKey] = obj;
}
}
products.append('product_image', JSON.stringify(parentObj));
parentObj = {};
for (var i = 0; i < sizeValue.length; i++) {
console.log(sizeValue[i].no);
var obj = {
"no": sizeValue[i].no,
"Name": sizeValue[i].Name,
"unit": sizeValue[i].unit,
"user": 2,
"secret_key": secret_size
};
parentObj[i] = obj;
}
products.append('product_size', JSON.stringify(parentObj));
parentObj = {};
for (var i = 0; i < stockValue.length; i++) {
var obj = {
"no": stockValue[i].no,
"Color": stockValue[i].Name,
"Stock": stockValue[i].unit,
"Offer": stockValue[i].Offer,
"Discount": stockValue[i].Discount,
"Mark_Price": stockValue[i].Mark_Price,
"Offer_Expiry_Date": stockValue[i].Offer_Expiry_Date,
"Price": stockValue[i].Price,
"parent": stockValue[i].parent,
"user": 2,
"secret_key": secret_color_stock
};
parentObj[i] = obj;
}
products.append('product_color_stock', JSON.stringify(parentObj));
const productsObj = {};
for (const [key, value] of products.entries()) {
if (key === 'product_size' || key === 'product_color_stock' || key === 'product_image') {
productsObj[key] = JSON.parse(value);
} else {
productsObj[key] = value;
}
}
console.log(productsObj);
try {
const res = await axios.post('http://127.0.0.1:8000/media/product/create/', products);
console.log("Product Created Successfully");
} catch (error) {
console.log(error);
console.log(error.response.data); // Accessing response data
}
}
As I have console Products is this how it looks like
{
"secret_key": "nISl5c?$U5hq-rAdetails1",
"user": "2",
"Name": "1",
"category": "1",
"Description": "1",
"archive": "false",
"size_Description": "1",
"color_Description": "1",
"Main_Image": {},
"mainImage": "blob:http://localhost:3000/efab0902-3d2e-41e7-9a1f-8a9d1533754d",
"Banner": {},
"bannerImage": "blob:http://localhost:3000/aba8135c-1a3b-4d02-9e6f-7117153ab76d",
"product_image": {
"Img[0]": {
"Name": "Img[0]",
"user": 2,
"image": {},
"secret_key": "nISl5c?$U5hq-rAimage1"
},
"Img[1]": {
"Name": "Img[1]",
"user": 2,
"image": {},
"secret_key": "nISl5c?$U5hq-rAimage1"
}
},
"product_size": {
"0": {
"no": "g9rYLTrtoskqQ6QphNeej",
"Name": "XL",
"unit": "1",
"user": 2,
"secret_key": "nISl5c?$U5hq-rAsize1"
}
},
"product_color_stock": {
"0": {
"no": "TjNgS4eFLNNHtWHaPgY2f",
"Offer": "Discount_Percentage",
"Discount": 1,
"Mark_Price": 8,
"Offer_Expiry_Date": "2024-05-17",
"Price": 7.92,
"parent": "g9rYLTrtoskqQ6QphNeej",
"user": 2,
"secret_key": "nISl5c?$U5hq-rAcolor_stock1"
}
}
}
As i Console the Error and it looks like though i have product_color_stock, product_size, product_image still i am facing there errors
{
"product_color_stock": [
"This field is required."
],
"product_size": [
"This field is required."
],
"product_image": [
"This field is required."
]
}