Home > Blockchain >  I am facing issue while executing JSONRenderer.render(serialized_student_data.data) in Django
I am facing issue while executing JSONRenderer.render(serialized_student_data.data) in Django

Time:05-06

I am new to Django, Whenever i execute the line json_data = JSONRenderer.render(serialized_student_data.data) it gives me error like TypeError: render() missing 1 required positional argument: 'data'

#Here is code in the models.py

class StudentModel(models.Model):
    name = models.CharField(max_length=100, default='user')
    roll_no = models.IntegerField(default=1)
    city = models.CharField(max_length=100, default='Ahmedabad')

#Here is code in the serializers.py

class StudentSerializer(serializers.Serializer):
    name = serializers.CharField(max_length=100)
    roll_no = serializers.IntegerField()
    city = serializers.CharField(max_length=100)

#Here is code in the views.py

def student_view(request):
    student_object = StudentModel.objects.get(id=3)
    serialized_student_data = StudentSerializer(student_object)
    json_data = JSONRenderer.render(serialized_student_data.data)
    return HttpResponse(json_data, content_type='application/json')

CodePudding user response:

You should create JSONRender object instead of use class, and the code will be,

    json_data = JSONRenderer().render(data=serialized_student_data.data)
    
  • Related