25

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?

3
  • "And I'm still getting errors. But I don't get the error message anywhere" isn't that contradictory? Commented Jun 5, 2017 at 12:56
  • I'm getting an error 500 in the browser, but I don't see what is causing such error. Commented Jun 5, 2017 at 12:59
  • you would obviously need to look in the log file Commented Jun 5, 2017 at 13:00

4 Answers 4

30

You could use the 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 list field classes.

You could write a custom field for serializer inheriting from ListField form the drf serializers which accepts list of strings. Maybe like this, this example is already shown in the DRF docs.

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

We can now reuse our custom StringListField class throughout our application, without having to provide a child argument to it.

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

You could use the custom field in your serializer like,

class InstalledAppsSerializer(serializers.Serializer):

    name = serializers.CharField(max_length=256)

    child = serializers.CharField()

    installed_apps_field = StringListField()
Sign up to request clarification or add additional context in comments.

Comments

9

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

Comments

2

The 'all kinds of errors' would probably go away if you based your serializer on a serializer, rather than a serializer field

ListField

A field class that validates a list of objects.

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

class InstalledAppsSerializer(serializers.Serializer):

    name = serializers.CharField(max_length=256)

    child = serializers.CharField()

    installed_apps_field = serializers.SerializerMethodField(
        'get_installed_apps')

1 Comment

Thanks @e4c5 I updated my answer with how I have changed my code. Any help would be appreciated
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.