How to Send List of IDs via multipart/form-data in Django REST Framework

I am working on a Django REST Framework API where I need to send a list of IDs in a multipart/form-data request to create a many-to-many relationship in the database. While everything works perfectly when using JSON as the request format, I face issues when switching to multipart/form-data because the list of IDs doesn't serialize correctly.

Here's my serializer:

class AddOrUpdateContractSerializer(serializers.ModelSerializer):
    deputy_agent = serializers.ListField(
        child=serializers.IntegerField(), required=False, allow_empty=False
    )
    product_type = serializers.ListField(
        child=serializers.IntegerField(), required=False, allow_empty=False
    )
    referral_type = serializers.ListField(
        child=serializers.IntegerField(), required=False, allow_empty=False
    )
    customer_id = serializers.IntegerField(required=False)

    class Meta:
        model = Contract
        exclude = ["id", "product", "is_deleted", "state", "customer"]

Here's my view

class AddContract(APIView):
    @extend_schema(request=AddOrUpdateContractSerializer, tags=["contract"])
    def post(self, request):
        serialized_data = AddOrUpdateContractSerializer(data=request.data)
        if serialized_data.is_valid(raise_exception=True):
            service = ContractService(
                serialized_data=serialized_data.validated_data, user=request.user
            )
            service.create_contract()
            return Response(
                status=status.HTTP_200_OK,
                data={"detail": "contract created successfully"},
            )

And the service for creating the contract:

def create_contract(self):
    items = ["customer_id", "deputy_agent", "product_type", "referral_type"]

    customer = CustomerSelector.get_customer_by_id(
        self.serialized_data.get("customer_id", None)
    )
    deputy_agent = self.serialized_data.get("deputy_agent", None)
    product_type = self.serialized_data.get("product_type", None)
    referral_type = self.serialized_data.get("referral_type", None)

    self.remove_unnecessary_items(items)

    contract = Contract(customer=customer, **self.serialized_data)

    contract.full_clean()
    contract.save()

    if deputy_agent:
        deputy_agents = DeputyAgent.objects.filter(id__in=deputy_agent)
        contract.deputy_agent.add(*deputy_agents)

    if product_type:
        product_types = ComboBoxsOptions.objects.filter(id__in=product_type)
        contract.product_type.add(*product_types)

    if referral_type:
        referral_types = ComboBoxsOptions.objects.filter(id__in=referral_type)
        contract.referral_type.add(*referral_types)

PROBLEM

When sending the data as multipart/form-data, lists like deputy_agent, product_type, and referral_type are received as strings instead of lists

Why I Need multipart/form-data I cannot use JSON because I also need to upload image files along with these IDs in the same request.

Example Request

Content-Type: multipart/form-data

customer_id: 1  
deputy_agent: [1, 2, 3]  
product_type: [4, 5, 6]  
image: <uploaded_file>

Question How can I handle lists of IDs in multipart/form-data properly in Django REST Framework? Is there a best practice or workaround for this issue?

Вернуться на верх