How to upload a CSV file in Django using the REST API?

Viewed 2911

How can I send a 220,000-line CSV file to Django using the Rest Framework API? Thank you.

3 Answers

@Botta- How about something like this from django.db import models

Create your models here.

class MyFile(models.Model):
    file = models.FileField(blank=False, null=False,upload_to='images/')
    description = models.CharField(null=True,max_length=255)
    uploaded_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural = 'MyFiles'


class MyFileView(APIView):
    # MultiPartParser AND FormParser
    # https://www.django-rest-framework.org/api-guide/parsers/#multipartparser
    # "You will typically want to use both FormParser and MultiPartParser
    # together in order to fully support HTML form data."
    parser_classes = (MultiPartParser, FormParser)

    def post(self, request, *args, **kwargs):
        file_serializer = MyFileSerializer(data=request.data)
        if file_serializer.is_valid():
            file_serializer.save()
            return Response(file_serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

serializers.py

from rest_framework import serializers
from .models import MyFile


class MyFileSerializer(serializers.ModelSerializer):
    class Meta:
        model = MyFile
        fields = ('file', 'description', 'uploaded_at')

@Stacy Hi. I made the changes, but it didn't work. I would like the fields in the CSV file to go automatically to my models and to the REST API. I would like to import my CSV data when uploading the file. Thanks.

models.py

from django.db import models


class MyFile(models.Model):
   file = models.FileField(blank=False, null=False,upload_to='images/')
   description = models.CharField(null=True,max_length=255)
   uploaded_at = models.DateTimeField(auto_now_add=True)
   name = models.CharField(max_length=150)
   sex = models.CharField(max_length=150)
   age = models.CharField(max_length=50)
   height = models.CharField(max_length=150)

   def __str__(self):
    return self.description

   class Meta:
     verbose_name_plural = 'MyFiles'

serializers.py

from rest_framework import serializers
from .models import MyFile


class MyFileSerializer(serializers.ModelSerializer):
 class Meta:
    model = MyFile
    fields = ("file", "description", "uploaded_at", "name", "sex", "age", "height",)

@Stacy- Hello. I was able to upload the csv file and pass it to my API as .CSV, however, I would like all fields in my CSV file to be transferred to API Rest. For example ID, Name, Age and Profile. It's possible? Thank you. Thank you.

serializers.py

from rest_framework import serializers
from .models import MyFile


class MyFileSerializer(serializers.ModelSerializer):
 class Meta:
    model = MyFile
    fields = ('file', 'description', 'uploaded_at')

models.py

from django.db import models


class MyFile(models.Model):
 file = models.FileField(blank=False, null=False,upload_to='images/')
 description = models.CharField(null=True,max_length=255)
 uploaded_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
    return self.description

class Meta:
    verbose_name_plural = 'MyFiles'

views.py

from rest_framework.parsers import MultiPartParser, FormParser

from .models import MyFile
from .serializers import MyFileSerializer

from rest_framework import generics
from rest_framework import viewsets
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.exceptions import NotFound
from rest_framework.response import Response
from rest_framework.views import APIView


class MyFileViewSet(viewsets.ModelViewSet):
 queryset = MyFile.objects.all()
 serializer_class = MyFileSerializer

urls.py

from .views import MyFileViewSet

from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'', MyFileViewSet)
urlpatterns = router.urls
Related