Search code examples
pythondjangodjango-rest-frameworkmongoenginebrowsable

List field input in django rest frameowrk browsable api - mongoengine


I have created a serializer that takes a list argument tags, but on the django-rest-framework browsable api, It doesn't seem to work.

Code:

Model

class SocialFeed(Document):
    platform = StringField(max_length=20, required=True, choices=('facebook', 'twitter', 'instagram'))
    tags = ListField(default=None)
    created_at = DateTimeField(default=datetime.now(), required=True)

Serializer

class SocialFeedCreateSerializer(DocumentSerializer):
    class Meta:
        model = SocialFeed
        fields = [
            'id',
            'platform',
            'tags'
        ]

View

class SocialFeedCreateAPIView(CreateAPIView):
    queryset = SocialFeed.objects.all()
    serializer_class = SocialFeedCreateSerializer

But on the browsable API, It shows a simple input box to enter tags, and I don't know what format should I put the tags in on the browsable API side, and I am not receiving list of tags instead a string.

I have tried following inputs:

#1 - ['social media', 'digital media', 'digital']
#2 - 'social media', 'digital media', 'digital'
#3 -  social media, digital media, digital
#4 - "social media", "digital media", "digital"

But on the MongoDb, when I fetch the document, It shows string instead a list of tags, like this:

"tags" : [
    "['social media','digital media','digital']"
]

"tags" : [
    "'social media','digital media','digital'"
]

"tags" : [
    "social media, digital media, digital]"
]

"tags" : [
    "\"social media\", \"digital media\", \"digital\""
]

Required Output

"tags" : [
    "social media",
    "digital media",
    'digital"
]

If anybody has faced the same issue, please guide me.


Solution

  • DRF does only support Django's fields. You likely need to make some fields more explicits in the serializer's declaration, like making tags inherit from a serializer.ListField.