Discussion:
Django Rest Framework File Upload
ray
2018-04-30 18:20:45 UTC
Permalink
Hi everyone.I need to upload a file using the Rest Framework API.Im looking
to extract then list the metadata (package_name & package version code)
from an android application(apk file).

This is what i have so far

<Loading Image...>
Here is my code so far. I can only do this through the admin, but i want it
in such a way that when when file is uploaded,the serializer can
get/extract the fields(package_name & package version code).


models.py

from django.db import models

class Applist(models.Model):
application = models.FileField(upload_to = 'uploads/')
package_name = models.CharField(max_length=200)
package_version_code = models.IntegerField(default=0)


serializers.py



import os
from rest_framework import serializersfrom . import models


class FileUploaderSerializer(serializers.ModelSerializer):
class Meta:
model = models.Applist
fields = ('application', 'package_name', 'package_version_code',)

def validate(self, validated_data):
validated_data['name'] = os.path.splitext(validated_data['file'].name)[0]
#other validation logic
return validated_data

def create(self, validated_data):
return FileUploader.objects.create(**validated_data)






views.py


from rest_framework import viewsetsfrom rest_framework.parsers import FormParser, MultiPartParser

from . import modelsfrom . import serializers



class FileUploaderViewSet(viewsets.ModelViewSet):
serializer_class = serializers.FileUploaderSerializer
parser_classes = (MultiPartParser, FormParser,)
queryset = models.Applist.objects.all()

def get_queryset(self, *args, **kwargs):
qs = super(FileUploaderViewSet, self).get_queryset(*args, **kwargs)
# qs = qs.filter(owner=self.request.user)
return qs
--
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.
Prasanna Balaraman
2018-04-30 18:48:26 UTC
Permalink
Follow the steps mentioned in this blog and you should be able to
accomplish it.
https://blog.vivekshukla.xyz/uploading-file-using-api-django-rest-framework/

Thanks,

[image: freshworks-dew.png]

Prasanna Balaraman

Team Enabler

p : +91-9940112996

e : ***@freshworks.com

w : www.freshworks.com <http://www.freshdesk.com/>

[image: fb.png] <http://www.freshdesk.com/> [image: twitter.png]
<https://twitter.com/prasannab> [image: linkedin.png]
<https://www.linkedin.com/in/prasanna-balaraman-8111ab6/>
Post by ray
Hi everyone.I need to upload a file using the Rest Framework API.Im
looking to extract then list the metadata (package_name & package version
code) from an android application(apk file).
This is what i have so far
<https://lh3.googleusercontent.com/-72tCyZs5-GM/WudbhIa4RZI/AAAAAAAAAN0/x99WnpLADzU0N7dP8nub8vWIaLpYCwUNwCLcBGAs/s1600/screenshot.png>
Here is my code so far. I can only do this through the admin, but i want
it in such a way that when when file is uploaded,the serializer can
get/extract the fields(package_name & package version code).
models.py
from django.db import models
application = models.FileField(upload_to = 'uploads/')
package_name = models.CharField(max_length=200)
package_version_code = models.IntegerField(default=0)
serializers.py
import os
from rest_framework import serializersfrom . import models
model = models.Applist
fields = ('application', 'package_name', 'package_version_code',)
validated_data['name'] = os.path.splitext(validated_data['file'].name)[0]
#other validation logic
return validated_data
return FileUploader.objects.create(**validated_data)
views.py
from rest_framework import viewsetsfrom rest_framework.parsers import FormParser, MultiPartParser
from . import modelsfrom . import serializers
serializer_class = serializers.FileUploaderSerializer
parser_classes = (MultiPartParser, FormParser,)
queryset = models.Applist.objects.all()
qs = super(FileUploaderViewSet, self).get_queryset(*args, **kwargs)
# qs = qs.filter(owner=self.request.user)
return qs
--
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.
ray
2018-04-30 19:04:11 UTC
Permalink
Thanks, but then blog explains what i've already done. You enter the fields
explicitly. What i need is when you upload a file, the API extracts the
package name and package_version_code and displays as shown in the image
above.

Any idea how i should modify the serializer?

Thanks.
Post by Prasanna Balaraman
Follow the steps mentioned in this blog and you should be able to
accomplish it.
https://blog.vivekshukla.xyz/uploading-file-using-api-django-rest-framework/
Thanks,
[image: freshworks-dew.png]
Prasanna Balaraman
Team Enabler
p : +91-9940112996
w : www.freshworks.com <http://www.freshdesk.com/>
[image: fb.png] <http://www.freshdesk.com/> [image: twitter.png]
<https://twitter.com/prasannab> [image: linkedin.png]
<https://www.linkedin.com/in/prasanna-balaraman-8111ab6/>
Post by ray
Hi everyone.I need to upload a file using the Rest Framework API.Im
looking to extract then list the metadata (package_name & package version
code) from an android application(apk file).
This is what i have so far
<https://lh3.googleusercontent.com/-72tCyZs5-GM/WudbhIa4RZI/AAAAAAAAAN0/x99WnpLADzU0N7dP8nub8vWIaLpYCwUNwCLcBGAs/s1600/screenshot.png>
Here is my code so far. I can only do this through the admin, but i want
it in such a way that when when file is uploaded,the serializer can
get/extract the fields(package_name & package version code).
models.py
from django.db import models
application = models.FileField(upload_to = 'uploads/')
package_name = models.CharField(max_length=200)
package_version_code = models.IntegerField(default=0)
serializers.py
import os
from rest_framework import serializersfrom . import models
model = models.Applist
fields = ('application', 'package_name', 'package_version_code',)
validated_data['name'] = os.path.splitext(validated_data['file'].name)[0]
#other validation logic
return validated_data
return FileUploader.objects.create(**validated_data)
views.py
from rest_framework import viewsetsfrom rest_framework.parsers import FormParser, MultiPartParser
from . import modelsfrom . import serializers
serializer_class = serializers.FileUploaderSerializer
parser_classes = (MultiPartParser, FormParser,)
queryset = models.Applist.objects.all()
qs = super(FileUploaderViewSet, self).get_queryset(*args, **kwargs)
# qs = qs.filter(owner=self.request.user)
return qs
--
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...