Skip to main content

A DRF app upload files easily

Project description

DRF File Upload

A reusable django library to handle file upload with the Django Rest Framework.

It provides views, serializers and models for simplifying file uploads and their model association in your RESTful application.

How it works

  1. Upload the file using this library multi-part APIs:

    POST https://example.com/api/upload/
    # A multipart request with a `file` field that contains your file
  2. If upload is complete, an unique identifier for that file is returned, along an URL for accessing it:

    {
       "url": "https://example.com/media/upload/file.png",
       "uuid: "1ad29aa9-d470-442d-a5a3-5922e7ce0182"
    }
  3. Use the uuid in your APIs for associating the uploaded file with your django model instance:

    POST https://example.com/api/foo/
    {
       [...],
       "my-file-attribute": "1ad29aa9-d470-442d-a5a3-5922e7ce0182"
    }
  4. If you want to update the resource but leave the file unchanged, simply pass the file url as value:

    PUT https://example.com/api/foo/2/
    {
        [...],
        "my-file-attribute": "https://example.com/media/upload/file.png"
    }

Quick start

  1. Add “drf_file_upload” to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
        ...
        'drf_file_upload',
    ]
  2. Include the polls URLconf in your project urls.py like this:

    path('upload/', include('drf_file_upload.urls')),

This will add both separate authenticated and anonymous users to file upload endpoints. Todo: add single view examples

  1. Add the UploadedFileField to your serializers todo

  2. Run python manage.py migrate to create the file upload models.

  3. Run the cleanup management command deleted_expired_uploaded_files in a cron task or add a celery task

TODO: Improve https://docs.djangoproject.com/en/3.1/intro/reusable-apps/

TODOs & IDEAS

  • Add support for Image file

  • check if DRF dedicated fields can be exploited

  • lots of config (e.g. permission_classes)

  • check if clean_uploaded_files can be called somewhere else to avoid save method override

  • Documentation!

  • Check if the file field can be set globally for all model FileFields

  • Add better spectacle openapi docs

  • Add missing tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_file_upload-0.1.4.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

drf_file_upload-0.1.4-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file drf_file_upload-0.1.4.tar.gz.

File metadata

  • Download URL: drf_file_upload-0.1.4.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for drf_file_upload-0.1.4.tar.gz
Algorithm Hash digest
SHA256 4a5daf5d906ebc941159080774293f8ef36c7f6dcf11b2003f0604d8abb023cb
MD5 83224a2ec452f9479c87d07c0728877f
BLAKE2b-256 f73f64626d9cb44001a4769eb4194be32e4f5e1a8793c5e89ae6d9b83eb86ef5

See more details on using hashes here.

File details

Details for the file drf_file_upload-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_file_upload-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 615baf4d0476aa6902d6b9217115a21d2280c40c04a93756416d547384da3eb1
MD5 0ef5f936c4a567d48ec1cf24aa133986
BLAKE2b-256 8b9bcfe1354f7e8845acb8bbccdcf58b5dedbc023feb06c3c52882852c0f8521

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page