Я получаю ошибку Expecting value: line 1 column 1 (char 0) when trying to DeSerialization and Insert Data Django REST Framework
I am getting error Expecting value: line 1 column 1 (char 0) when trying to DeSerialization and Insert Data Django REST Framework.
Я перепробовал все рекомендации из stackoverflow, но они не работают, пожалуйста, помогите. Мой код `
Models.py
'''
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
# Create your models here.
class Products(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=10, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
rating = models.FloatField(validators=[MinValueValidator(0.0), MaxValueValidator(10.0)])
image = models.FileField(default="")
total_participant = models.IntegerField()
participant_price = models.DecimalField(max_digits=10, decimal_places=2)
is_delete = models.BooleanField(default=False)
I am getting error Expecting value: line 1 column 1 (char 0) when trying to DeSerialization and Insert Data Django REST Framework.
'''
Views.py
'''
from django.shortcuts import render
импортировать io
from rest_framework.parsers import JSONParser
from .serializers import ProductsSerializer
from rest_framework.renderers import JSONRenderer
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
@csrf_exempt
def product_create(request):
# ist check post request
if request.method == 'POST':
# request body data store in json_data
json_data = request.body
# then stream the data
stream = io.BytesIO(json_data)
# then parse data convert to python_data
python_data = JSONParser().parse(stream)
# then python_data convert to complex data
serializer = ProductsSerializer(data=python_data)
# then check our data is valid or not
if serializer.is_valid():
serializer.save()
# then return response to clint through message
res = {'msg': 'Data Created'}
json_data = JSONRenderer().render(res)
return HttpResponse(json_data, content_type='application/json')
json_data = JSONRenderer().render(serializer.errors)
return HttpResponse(json_data, content_type='application/json')
json_data = JSONRenderer().render(serializer.errors)
return HttpResponse(json_data, content_type='application/json')
'''
Serializer file
serializers.py
'''
from rest_framework import serializers
from .models import *
class ProductsSerializer(serializers.Serializer):
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=10, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
rating = models.FloatField(validators=[MinValueValidator(0.0), MaxValueValidator(10.0)])
image = models.FileField(default="")
total_participant = models.IntegerField()
participant_price = models.DecimalField(max_digits=10, decimal_places=2)
is_delete = models.BooleanField(default=False)
def create(self, validate_data):
return Products.objects.create(**validate_data)
''' Я получаю ошибку Expecting value: line 1 column 1 (char 0) when trying to DeSerialization and Insert Data Django REST Framework.
simple python app where i insert the data
myapp.py
'''
import requests
import json
UrL= "http://127.0.0.1:8000/pcreate/"
data = {
'name' : 'Umer',
'price': '3000',
'created_at' : 'True',
'updated_at' : 'True',
'rating' : '3.4',
'image' : '',
'total_participant' : '4',
'participant_price' : '4000',
'is_delete' : 'True',
}
json_data = json.dumps(data)
r = requests.post(url= UrL, data= json_data)
data = r.json()
print(data)
''' Я получаю ошибку Expecting value: line 1 column 1 (char 0) when trying to DeSerialization and Insert Data Django REST Framework.
Traceback:
PS F:\Git\DjangoRestFramework\Rest_Framework_Practice2> python myapp.py
Traceback (most recent call last):
File "F:\Git\DjangoRestFramework\Rest_Framework_Practice2\myapp.py", line 21, in <module>
data = r.json()
File "C:\Users\ibsof\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 910, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\ibsof\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\ibsof\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\ibsof\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I am getting error Expecting value: line 1 column 1 (char 0) when trying to DeSerialization and Insert Data Django REST Framework.
I have an educational project that perfectly works on web version of Python, but once I tryed to run it on Pycharm I got this error JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I tryed all recomendations from stackoverflow they doesn't work, please help. My code `