Let's say I have a model like
class MyModel(models.Model):
kwargs = models.TextField()
where the kwargs field is usually a JSON string, representing a dictionary of keyword arguments, e.g. '{"X": 1, "Y": 2}'. It's not practical to modify this model, as it's a model belonging to a 3rd party app, and modifying the field would probably require a fork and other changes in the code.
Now, I'm using django-rest-framework to provide and endpoint for creating/updating/listing/deleting instances of MyModel, and I have a serializer something like:
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = ('kwargs',)
to facilitate this. This all works OK, but for various reasons the user does not want to have manually enter the JSON dump string of the kwargs, i.e. manually entering '{"X": 1, "Y": 2}' in the text box or passing that manually built string. They'd rather be able to individually pass X and Y to the API.
How can I achieve this with a serializer? In other words how can I create a serializer with non-model fields offered to the user, but which get dumped/combined into a JSON string on update/create and mapped to the kwargs model instance field accordingly.