Skip to content
Advertisement

How can I fix my REST view to PATCH and serialize updates to an ArrayField() in my model?

I have a view that is used to update a field in my model. It’s represented as follows: stock_list = ArrayField(models.CharField())

Each value in the ArrayField is separated by commas.

I used a custom serializer method to allow for my backend to separate the elements in my PATCH obj by commas.

  • serializers.py:
JavaScript

Below is my view that I use, the URL’s are linked up correctly, however I’m setting up my view wrong:

  • view.py:
JavaScript

Here is the PATCH error I get:

JavaScript

I’m not sure why my view is expecting a dictionary, I only want the users to PATCH the arrayfield known as stock_list with the data they input.

Appreciate any debugging assistance here to get my PATCH view working properly as expected.

Advertisement

Answer

First, you need to change the super() method calling of to_internal_value(...) method (you were not calling it correctly)

JavaScript

and then, use the generics.RetrieveUpdateAPIView “as-is”, because, you don’t need any alterations to the view (at-least the minimal case you have given in the OP)

So, your view will become,

JavaScript

This will let you update the data with ease and keep in mind that the DRF expect the data in the following format,

JavaScript

Example Result ScreenShot Example Result ScreenShot

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement