21 Eylül 2018 Cuma 15:12:31 UTC+3 tarihinde Mustafa Çakıroğlu yazdı:
>
> [image: cb73ae20-ab83-4660-ae61-255290059996.png]
> I have a Django rest api which you can upload a file and an id. I want to 
> call a function when the post button (check the image if you want) is 
> pressed in order to do some stuff with the uploaded file. How can I do this 
> in django rest platform? Thanks in advance.
>
> Little bit more detailed information: I will upload a text file and when I 
> pressed the post button, I want a function to start (which reads file's 
> contents and finds a string in there).
>

*models.py*

from django.db import models
from .validators import validate_file_extension

class Uplist(models.Model):
    report_id = models.CharField(max_length=200, primary_key= True)
    application = models.FileField(blank=False, null=False, 
validators=[validate_file_extension] )
 
*serializers.py*

from rest_framework import serializers
from api import models


class UplistSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Uplist
        fields = ('application', 'report_id',)


*urls.py*

from django.urls import path
from django.conf.urls import url
from . import views

urlpatterns = [
    path('', views.ListUps.as_view()),
    url(r'^$', views.ListUps.as_view()),
    url(r'^(?P<pk>[0-9]+)/$', views.ListDetails.as_view()),
]

*views.py*

from rest_framework import generics
from rest_framework.parsers import MultiPartParser, FormParser

from api import models
from . import serializers


class ListUps(generics.ListCreateAPIView):
    parser_classes = (MultiPartParser, FormParser)
    queryset = models.Uplist.objects.all()
    serializer_class = serializers.UplistSerializer

class ListDetails(generics.RetrieveUpdateDestroyAPIView):
    queryset = models.Uplist.objects.all()
    serializer_class = serializers.UplistSerializer

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to