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?