Discussion:
Return JSON from view
Alexander Teut
2018-06-08 16:31:25 UTC
Permalink
I have a document model in my Django REST App.


class Document(models.Model):
template = models.ForeignKey(DocumentTemplate, blank=True, null=True,
on_delete=models.SET_NULL)

class DocumentFile(models.Model):
document = models.ForeignKey(Document, on_delete=models.CASCADE,
related_name='files')
datestamp = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=256, blank=True, null=True)
document_file = models.FileField(upload_to='uploads/%Y/%m/%d/',
blank=True)

Sometimes a document instance should be created for a couple of uploaded
files.

So, I create a special uploader view:

class DocumentUploadViewSet(APIView):
parser_classes = (MultiPartParser, FormParser)
rendrer_classes = (JSONRenderer, )

def post(self, request, *args, **kwargs):
files = request.FILES.getlist('files')
document_id = request.POST['document']
template_id = request.POST['template']
if document_id == 'null': document_id = None
if document_id is None:
document = Document.objects.create(template_id=template_id)
document_id = document.id
else:
Document.objects.update({
"id": document_id,
"template": template_id
})
document = Document.objects.get(id=document_id)
for file in files:
newdoc = DocumentFile(document_file=file, document=document,
name=file.name)
newdoc.save()
document = Document.objects.get(id=document_id)

document_json=JSONRenderer().render(DocumentSerializer(document).data)

return Response(document_json, status=status.HTTP_201_CREATED)

It uploads just fine, but returns... and HTML page from template instead of
pure JSON

How to make my post method return just JSON, not HTML stuff? I've tendted
document_json - it looks fine.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jason
2018-06-09 16:26:00 UTC
Permalink
you have a typo with *rendrer_classes.* it should be *renderer_classes*
Post by Alexander Teut
I have a document model in my Django REST App.
template = models.ForeignKey(DocumentTemplate, blank=True, null=True,
on_delete=models.SET_NULL)
document = models.ForeignKey(Document, on_delete=models.CASCADE,
related_name='files')
datestamp = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=256, blank=True, null=True)
document_file = models.FileField(upload_to='uploads/%Y/%m/%d/',
blank=True)
Sometimes a document instance should be created for a couple of uploaded
files.
parser_classes = (MultiPartParser, FormParser)
rendrer_classes = (JSONRenderer, )
files = request.FILES.getlist('files')
document_id = request.POST['document']
template_id = request.POST['template']
if document_id == 'null': document_id = None
document = Document.objects.create(template_id=template_id)
document_id = document.id
Document.objects.update({
"id": document_id,
"template": template_id
})
document = Document.objects.get(id=document_id)
newdoc = DocumentFile(document_file=file, document=document,
name=file.name)
newdoc.save()
document = Document.objects.get(id=document_id)
document_json=JSONRenderer().render(DocumentSerializer(document).data)
return Response(document_json, status=status.HTTP_201_CREATED)
It uploads just fine, but returns... and HTML page from template instead
of pure JSON
How to make my post method return just JSON, not HTML stuff? I've tendted
document_json - it looks fine.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alexander Teut
2018-06-11 09:18:39 UTC
Permalink
HI!

Thank you!

JsonResponse helped me :)
Post by Jason
you have a typo with *rendrer_classes.* it should be *renderer_classes*
Post by Alexander Teut
I have a document model in my Django REST App.
template = models.ForeignKey(DocumentTemplate, blank=True, null=True,
on_delete=models.SET_NULL)
document = models.ForeignKey(Document, on_delete=models.CASCADE,
related_name='files')
datestamp = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=256, blank=True, null=True)
document_file = models.FileField(upload_to='uploads/%Y/%m/%d/',
blank=True)
Sometimes a document instance should be created for a couple of uploaded
files.
parser_classes = (MultiPartParser, FormParser)
rendrer_classes = (JSONRenderer, )
files = request.FILES.getlist('files')
document_id = request.POST['document']
template_id = request.POST['template']
if document_id == 'null': document_id = None
document = Document.objects.create(template_id=template_id)
document_id = document.id
Document.objects.update({
"id": document_id,
"template": template_id
})
document = Document.objects.get(id=document_id)
newdoc = DocumentFile(document_file=file, document=document,
name=file.name)
newdoc.save()
document = Document.objects.get(id=document_id)
document_json=JSONRenderer().render(DocumentSerializer(document).data)
return Response(document_json, status=status.HTTP_201_CREATED)
It uploads just fine, but returns... and HTML page from template instead
of pure JSON
How to make my post method return just JSON, not HTML stuff? I've tendted
document_json - it looks fine.
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Regards,
Alexander Teut

http://rikkimongoose.ru
http://github.com/rikkimongoose
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Pallavi Singh
2018-06-11 09:16:38 UTC
Permalink
hey,will you please guide me ,how you are posting your query in group?
i need to post but i m not able to post.
so,please help.
Post by Alexander Teut
I have a document model in my Django REST App.
template = models.ForeignKey(DocumentTemplate, blank=True, null=True,
on_delete=models.SET_NULL)
document = models.ForeignKey(Document, on_delete=models.CASCADE,
related_name='files')
datestamp = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=256, blank=True, null=True)
document_file = models.FileField(upload_to='uploads/%Y/%m/%d/',
blank=True)
Sometimes a document instance should be created for a couple of uploaded
files.
parser_classes = (MultiPartParser, FormParser)
rendrer_classes = (JSONRenderer, )
files = request.FILES.getlist('files')
document_id = request.POST['document']
template_id = request.POST['template']
if document_id == 'null': document_id = None
document = Document.objects.create(template_id=template_id)
document_id = document.id
Document.objects.update({
"id": document_id,
"template": template_id
})
document = Document.objects.get(id=document_id)
newdoc = DocumentFile(document_file=file, document=document,
name=file.name)
newdoc.save()
document = Document.objects.get(id=document_id)
document_json=JSONRenderer().render(DocumentSerializer(
document).data)
return Response(document_json, status=status.HTTP_201_CREATED)
It uploads just fine, but returns... and HTML page from template instead
of pure JSON
How to make my post method return just JSON, not HTML stuff? I've tendted
document_json - it looks fine.
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...