Skip to content

Conversation

@AghastyGD
Copy link
Owner

@AghastyGD AghastyGD commented May 3, 2025

Description

This pull request introduces robust support for file uploads using multipart/form-data and includes a custom middleware to handle PUT/PATCH requests for multipart routes. While the primary goal is to enable file upload functionality, the middleware was added to address challenges encountered with Django handling of multipart updates, even after its recent updates.


Key Changes

  1. Multipart Upload Functionality:

    • Added support for handling FileField and ImageField in Django models with multipart/form-data.
    • Automatically detects FileField and ImageField in models and generates routes for these fields using multipart/form-data.
    • Models without file fields continue to use application/json by default.
    • Introduced parameters for fine-grained control:
      • file_fields: Specify which fields in a model should use multipart/form-data.
      • use_multipart: Explicitly define whether create and update operations for specific models should use multipart/form-data.
      • auto_multipart: Automatically enable multipart/form-data for models with file fields (default: True).
  2. Custom Middleware for PUT/PATCH Handling:

    • Added ProcessPutPatchMiddleware to handle PUT and PATCH requests as POST for proper form data processing.
    • This middleware resolves issues encountered with Django handling of multipart updates, ensuring compatibility with both synchronous and asynchronous request handlers.
    • Middleware usage:
      MIDDLEWARE = [
          ...
          'lazy_ninja.middleware.ProcessPutPatchMiddleware',
          ...
      ]

Example Usage

auto_api = DynamicAPI(
    api,
    is_async=True,
    file_fields={"Gallery": ["images"], "Product": ["pimages"]},  # Specify file fields
    use_multipart={
        "Product": {
            "create": True,  # Use multipart/form-data for creation
            "update": True   # Use multipart/form-data for updates
        }
    },
    auto_multipart=True  # Automatically detect file fields and use multipart
)

Why This is Needed

  1. Enable File Uploads:

    • Provides a structured way to handle file uploads in Django models.
    • Supports mixed models where some routes use multipart/form-data and others use application/json.
    • Allows developers to override defaults and explicitly define behavior for specific models and operations.
  2. Custom Middleware:

    • Django Ninja's recent updates for handling multipart updates did not work as expected in this library's context.
    • The custom middleware ensures that PUT and PATCH requests with multipart/form-data are processed correctly, enabling seamless updates for models with file fields.

Next Steps

  • Expand documentation to include detailed examples of the new file_fields, use_multipart, and auto_multipart parameters.
  • Add more test cases to ensure compatibility with various model configurations.

This PR introduces essential functionality for file uploads while addressing multipart update challenges.

@AghastyGD AghastyGD added the enhancement New feature or request label May 3, 2025
@AghastyGD AghastyGD self-assigned this May 3, 2025
@AghastyGD AghastyGD merged commit 2be1181 into dev May 24, 2025
3 checks passed
@AghastyGD AghastyGD deleted the feature/file-upload-support branch July 5, 2025 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant