[Django rest framework]: Serialize a list of strings

Viewed 19241

I am working with django and djando rest framework

I have created a new endpoint installedapps. When making GET requests to it, I want to return the data contained as a list of strings (list of installed apps)

The list of strings looks something like this:

installed_apps = ['django_admin_bootstrapped', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.humanize', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'django_js_reverse', 'djcelery', 'bootstrap3', 'foo', 'bar', 'apirest']

Until now I have only worked with model serializers, and everything was pretty easy. But now i dont know how to return this list of strings

This is what I have tried so far:

class InstalledAppsViewSet(viewsets.ViewSet):
    serializer_class = serializers.InstalledAppsSerializer

    def list(self, request):
        from credits.views import GetInstalledApps

        installed_apps = GetInstalledApps.get_installed_apps()

        serializer = serializers.InstalledAppsSerializer(
            instance=installed_apps, many=True)

        return Response(serializer.data)




class InstalledAppsSerializer(serializers.ListField):

    name = serializers.CharField(max_length=256)

    child = serializers.CharField()

    installed_apps_field = serializers.SerializerMethodField(
        'get_installed_apps')

I am always getting all kind of errors. Any help on how to return the content of the list of strings?

Update

I have tried @e4c5 code, leaving it like this:

class InstalledAppsViewSet(viewsets.ViewSet):
    serializer_class = serializers.InstalledAppsSerializer

    def list(self, request):

        serializer = serializers.InstalledAppsSerializer


class InstalledAppsSerializer(serializers.Serializer):

    name = serializers.CharField(max_length=256)

    child = serializers.CharField()

    installed_apps = serializers.SerializerMethodField('get_the_installed_apps')


    def get_the_installed_apps(self):
        from credits.views import GetInstalledApps
        installed_apps = GetInstalledApps.get_installed_apps()

        return installed_apps

And I'm still getting errors. But I don't get the error message anywhere. Any help?

4 Answers

it works for serialize a list of strings

class MySerializer(serializers.Serializer):
    installed_apps = serializers.ListSerializer(child=serializers.CharField())

it return

['django_admin_bootstrapped', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.humanize', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'django_js_reverse', 'djcelery', 'bootstrap3', 'foo', 'bar', 'apirest']

For the

And I'm still getting errors. But I don't get the error message anywhere.

part of the question: the error should be in the response you receive from the view after sending the request.

If you have something like:
response = InstalledAppsViewSet.as_view()(request, **kwargs),

to print the content of the response:
response.render().content - the error should be there.

Related