Skip to main content

REST framework files

build-status-image coverage-status-image

File download and upload support for Django REST framework.


Overview

REST framework files allows you to download a file in the format used to render the response and also allows creation of model instances by uploading a file containing the model fields.

Requirements

  • Python (2.7, 3.5)

  • Django REST framework (3.4, 3.6)

Installation

Install using pip:

pip install djangorestframework-files

Example

models.py

from django.db import models

class ABC(models.Model):
    name = models.CharField(max_length=255)

serializers.py

from rest_framework import serializers

from .models import ABC

class ABCSerializer(serializers.ModelSerializer):
    class Meta:
        model = ABC
        fields = '__all__'

views.py

from rest_framework.parsers import JSONParser, MultiPartParser
from rest_framework_files.viewsets import ImportExportModelViewSet

from .models import ABC
from .serializers import ABCSerializer

class ABCViewSet(ImportExportModelViewSet):
    queryset = ABC.objects.all()
    serializer_class = ABCSerializer
    # if filename is not provided, the view name will be used as the filename
    filename = 'ABC'
    # renderer classes used to render your content. will determine the file type of the download
    renderer_classes = (JSONParser, )
    parser_classes = (MultiPartParser, )
    # parser classes used to parse the content of the uploaded file
    file_content_parser_classes = (JSONParser, )

Some third party packages that offer media type support:

urls.py

from rest_framework import routers

from .views import ABCViewSet

router = routers.ImportExportRouter()
router.register(r'abc', ABCViewSet)

urlpatterns = router.urls

Downloading

To download a json file you can go to the url /abc/?format=json. The format query parameter specifies the media type you want your response represented in. To download an xml file, your url would be /abc/?format=xml. For this to work, make sure you have the respective renderers to render your response.

Uploading

To create model instances from a file, upload a file to the url /abc/. Make sure the content of the file can be parsed by the parsers specified in the file_content_parser_classes or else it will return a HTTP_415_UNSUPPORTED_MEDIA_TYPE error.

For sample file examples you can upload, check the assets folder

For more examples on how to use the viewsets or generic views, check the test application

Metadata

Release files for djangorestframework-files 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for djangorestframework-files 1.0.1
File Size Uploaded
djangorestframework-files-1.0.1.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for djangorestframework-files 1.0.1
File Interpreter ABI Platform
djangorestframework_files-1.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 13.1 kB

Release files / djangorestframework-files-1.0.1.tar.gz

Download URL djangorestframework-files-1.0.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
76a031917f01d31585733a874e8a83ce3702935bdd5f9e5e99b12f66092353d9
BLAKE2b-256 checksum
How to use checksums
b803d43ba2437d1c37f7e6d1ee7952f1845ec103ef6950f021ed180170c54ecd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / djangorestframework_files-1.0.1-py2.py3-none-any.whl

Download URL djangorestframework_files-1.0.1-py2.py3-none-any.whl
Size 7.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b4a325e59a9c755728589ca70642eca463ea2c30e34a97cdb94936577979a75a
BLAKE2b-256 checksum
How to use checksums
0758fe94d8d0a4496675d33a96d43cd92540855f701b5603677c636f67b6cf77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

1.1.0

1 release file

This release

1.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page