Python/Django with REST Framework - Error on POST {"non_field_errors": ["Invalid data"]} -
i'm starting out django , rest framework , i'm getting error when trying post entity. there error is: {"non_field_errors": ["invalid data"]}. happens @ line serializer.is_valid()
this is view class looks like:
from django.http import httpresponse django.views.decorators.csrf import csrf_exempt rest_framework.renderers import jsonrenderer rest_framework.parsers import jsonparser api.models import payload api.serializers import payloadserializer api.services import payloadservice class jsonresponse(httpresponse): """ httpresponse renders content json. """ def __init__(self, data, **kwargs): content = jsonrenderer().render(data) kwargs['content_type'] = 'application/json' super(jsonresponse, self).__init__(content, **kwargs) @csrf_exempt def payload_list(request): if request.method == 'post': model = payload(code="754d", body="hello world") serializer = payloadserializer(data=model) if serializer.is_valid(): serializer.save() return jsonresponse(serializer.data, status=201) return jsonresponse(serializer.errors, status=400)
here payload model reference:
from __future__ import unicode_literals sqlserver_ado.fields import bigautofield django.db import models class payload(models.model): id = bigautofield(db_column='id', primary_key=true) # field name made lowercase. code = models.charfield(db_column='code', max_length=10) # field name made lowercase. body = models.textfield(db_column='body') # field name made lowercase. class meta: managed = false db_table = 'payload'
it's worth noting if set view , pass in json payload object part of request, works fine.
working json payload: {"code": "npmc", "body": "hello world"}
working view code:
if request.method == 'post': data = jsonparser().parse(request) serializer = payloadserializer(data=data) if serializer.is_valid(): serializer.save() return jsonresponse(serializer.data, status=201) return jsonresponse(serializer.errors, status=400)
response: {"id": 25, "code": "npmc", "body": "hello world"}
ok able find solution problem making use of djangos model_to_dict
from django.http import httpresponse django.views.decorators.csrf import csrf_exempt rest_framework.renderers import jsonrenderer rest_framework.parsers import jsonparser api.models import payload api.serializers import payloadserializer api.services import payloadservice django.forms.models import model_to_dict class jsonresponse(httpresponse): """ httpresponse renders content json. """ def __init__(self, data, **kwargs): content = jsonrenderer().render(data) kwargs['content_type'] = 'application/json' super(jsonresponse, self).__init__(content, **kwargs) @csrf_exempt def payload_list(request): if request.method == 'post': model = payload(code="754d", body="hello world") result = model_to_dict(model, fields=[], exclude=[]) serializer = payloadserializer(data=result) if serializer.is_valid(): serializer.save() return jsonresponse(serializer.data, status=201) return jsonresponse(serializer.errors, status=400)
Comments
Post a Comment