Hi. In your view instead of using Response(serializer) you need to use Response(serializer.data)
HTH. ma 8. heinäk. 2019 klo 19.48 'Amitesh Sahay' via Django users < [email protected]> kirjoitti: > hello Members, > > I am working on some POC for django rest framework. Its very simple setup, > nothing complicated. While I was doing my testing I am facing below error: > > =================================== > TypeError at /api/status/ > > Object of type 'ListSerializer' is not JSON serializable > > Request Method: GET > Request URL: http://127.0.0.1:8000/api/status/ > Django Version: 1.11.8 > Exception Type: TypeError > Exception Value: > > Object of type 'ListSerializer' is not JSON serializable > > Exception Location: /usr/lib/python3.6/json/encoder.py in default, line > 180 > Python Executable: /root/PycharmProjects/vrest/bin/python > Python Version: 3.6.5 > Python Path: > > ['/root/PycharmProjects/src', > '/usr/local/buildtools/current/sitecustomize', > '/usr/lib/python36.zip', > '/usr/lib/python3.6', > '/usr/lib/python3.6/lib-dynload', > '/root/PycharmProjects/vrest/lib/python3.6/site-packages'] > > Server time: Mon, 8 Jul 2019 16:03:26 +0000 > =================================== > Below are the details : > ----------------------------- > serializers.py > > from rest_framework import serializers > from STATUS.models import Status > > > class StatusSerializer(serializers.ModelSerializer): > class Meta: > model = Status > fields = [ > 'user', > 'content', > 'image' > ] > > def validate_content(self, value): > if len(value) > 500: > raise serializers.ValidationError("way too long string") > > def validate(self, data): > content = data.get('content', None) > if content =='': > content = None > image = data.get('image', None) > if content is None and image is None: > raise serializers.ValidationError('Required fields') > return data > > > models.py > ------------- > > from django.db import models > from django.conf import settings > > > def upload_status_image(instance, filename): > return "updates/{user}/{filename}" .format(user=instance.user, > filename=filename) > > > class StatusQuerySet(models.QuerySet): > pass > > > class StatusManager(models.Manager): > def get_queryset(self): > return StatusQuerySet(self.model, using=self._db) > > > class Status(models.Model): > user = models.ForeignKey(settings.AUTH_USER_MODEL) > content = models.TextField(null=True, blank=True) > image = models.ImageField(upload_to=upload_status_image) > updated = models.DateTimeField > timestamp = models.DateTimeField(auto_now_add=True) > > objects = StatusManager() > > def __str__(self): > return str(self.content)[:50] > > class Meta: > verbose_name = 'status post' > verbose_name_plural = 'status posts' > > views.py > > from django.views.generic import View > from rest_framework.views import APIView > from rest_framework.response import Response > from .serializers import StatusSerializer > from STATUS.models import Status > > > class StatusListSearchAPIView(APIView): > permission_classes = [] > authentication_classes = [] > > def get(self, request, format=None): > qs = Status.objects.all() > serializer = StatusSerializer(qs, many=True) > return Response(serializer) > > forms.py > > from django import forms > from .models import Status > > class StatusForm(forms.ModelForm): > class Meta: > model = Status > fields = [ > 'user', > 'content', > 'image' > ] > > def clean_content(self, *args, **kwargs): > content = self.cleaned_data.get('content') > if len(content) > 240: > raise forms.ValidationError('Content is too long') > return content > > def clean(self, *args, **kwargs): > data = self.cleaned_data > content = data.get('content', None) > if content == '': > content = None > > image = data.get('image', None) > if content is None and image is None: > raise forms.ValidationError('Content or image is required..') > return super().clean(*args, **kwargs) > > app.urls.py > > from django.conf.urls import url, include > from django.contrib import admin > from .views import StatusListSearchAPIView > > urlpatterns = [ > url(r'^$', StatusListSearchAPIView.as_view()), > #url(r'^create/$', StatusCreateAPIView.as_view()), > #url(r'^(?P<id>.*)/$', StatusDetailAPIiew.as_view()), > #url(r'^(?P<id>.*)/update/$', StatusUpdateAPIView.as_view()), > #url(r'^(?P<id>.*)/update/$', StatusDeleteAPIView.as_view()), > > ] > > project.urls.py > > from django.conf.urls import url, include > from django.contrib import admin > > urlpatterns = [ > url(r'^admin/', admin.site.urls), > url(r'api/status/', include('STATUS.API.urls')), > ] > > Project Structure : attached with the email, if not found do let me know I > will re-attach > > > > *Project name: google* > *APP name: STATUS* > *django version : 1.11* > *python version : 3.6* > *djangorestframework : 3.9.4* > > > Regards, > Amitesh Sahay > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/1812294846.4496775.1562604488272%40mail.yahoo.com > <https://groups.google.com/d/msgid/django-users/1812294846.4496775.1562604488272%40mail.yahoo.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHn91odiZ%2BhxOzvaF6Qb-g9vojW6uA4ruyHXeDmSHfxic%2BSYYQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

