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 : prasanna.balara...@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/>




On Mon, Apr 30, 2018 at 11:50 PM ray <rrup...@gmail.com> wrote:

> 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
>
> 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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to