Alexander Teut
2018-06-08 16:31:25 UTC
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.
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.
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.