Can I use Django REST with custom structures on serializers?

293
November 24, 2017, at 10:17 AM

I have a project in which I would like to be able to create a serializer that isn't based on a model and that could accept a custom structure of my choice.

Example:

class OptionSerializer(serializers.Serializer):
    option_1 = serializers.IntegerField()
    option_2 = serializers.IntegerField()
    option_3 = serializers.IntegerField()
    option_4 = serializers.IntegerField()
    option_5 = serializers.IntegerField()
    class Meta:
        fields = ('option_1', 'option_2', 'option_3', 'option_4', 'option_5')

class QuestionsSerializer(serializers.Serializer):
    questions = OptionSerializer(data=[], many=True)
    class Meta:
        fields = ('questions',)

In this example, I would have a custom object called Questions that would have an array of objects, each one containing the keys 'option_1', 'option_2', 'option_3', 'option_4' and 'option_5'. My goal would be to use something like this in a GenericAPIView that would allow me to render a similar structure when attempting a POST operation with the browsable API, and also to minimize validation efforts.

Is that possible to be done?

READ ALSO
how to extract a portion of data from text lines by python 2

how to extract a portion of data from text lines by python 2

I have a text file including a string header and data bodyThe contents seem like this:

218
Iterate through list in dictionary values in python

Iterate through list in dictionary values in python

I have a dictionary with almost 2000 keys, and every key has list as a value (with almost 2000 element in list)Something like this: dict

232
How to get Rss feed in python as per the specific keyword?

How to get Rss feed in python as per the specific keyword?

I have to do sentiment analysis for RSS feed of a news websiteHow can i get feed for a specific keyword ? so that they could be analyzed accordingly ?

250