[Django rest framework]: Serialize a list of strings

I am working with django and djando rest framework

I created a new endpoint installedapps`. When making GET requests, I want to return data contained in a list of strings (list of installed applications)

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']

      

So far I've only worked with model serializers and everything was pretty easy. But now I don't 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 always get all sorts of errors.

Any help on how to do this? Returns the contents of a list of strings

Update

I tried @ e4c5's 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 am still getting errors. But I am not getting the error anywhere. Any help?

+3


source to share


2 answers


You can use serializers.ListField

,

ListField is a field class that validates a list of objects.

The ListField class also supports a declarative style that allows you to write reusable field classes.

You can write a custom serializer field that inherits from the ListField

drf serializers form, which take a list of strings. Perhaps so, this example is already shown in the DRF docs.

class StringListField(serializers.ListField):
    child = serializers.CharField()

      



We can now reuse our own StringListField class in our application without providing it with a child argument.

This is from the docs, I haven't tried it yet. But I hope you get what you are looking for.

You can use a custom field in your serializer for example

class InstalledAppsSerializer(serializers.Serializer):

    name = serializers.CharField(max_length=256)

    child = serializers.CharField()

    installed_apps_field = StringListField()

      

+6


source


"All sorts of errors" will probably go away if you base your serializer on the serializer rather than the serializer field

ListField

The field class that validates the list of objects.



You might want to use it when one of your class members is a list. But you don't want the ListField to be used as a serializer because it doesn't have one

class InstalledAppsSerializer(serializers.Serializer):

    name = serializers.CharField(max_length=256)

    child = serializers.CharField()

    installed_apps_field = serializers.SerializerMethodField(
        'get_installed_apps')

      

+2


source







All Articles