1

I am creating rest api using Django Rest Framework. I am using Djanfo 2.0.5 and python 3. I am getting error "Object of type 'type' is not JSON serializable." at the time of submitting form in browser. I tried with POSTMAN and also getting same error. I have attached snaps of my code of model, serializers and view classes.

from django.db import models

# Create your models here.


class User(models.Model):

    full_name = models.CharField(max_length=255, blank=False)
    email = models.EmailField(blank=False, unique=True)
    password = models.CharField(max_length=100, blank=False)
    profile_pic = models.FileField()
    age = models.CharField(max_length=3, blank=False)
    location_lat = models.CharField(max_length=100, blank=False)
    location_long = models.CharField(max_length=100, blank=False)
    address = models.CharField(max_length=255, blank=False)
    experience_level = models.CharField(max_length=50, blank=False)
    utr_rating = models.CharField(max_length=50, blank=False)
    match_price = models.IntegerField
    auth_token = models.CharField(max_length=100, blank=False, unique=True)
    fb_token = models.CharField(max_length=100, unique=True)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

    def __str__(self):
        return "{}".format(self.full_name)
from rest_framework import serializers
from .models import User


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User

        fields = ('id',
                  'full_name',
                  'email',
                  'password',
                  'profile_pic',
                  'age',
                  'location_lat',
                  'location_long',
                  'address',
                  'experience_level',
                  'utr_rating',
                  'match_price',
                  'auth_token',
                  'fb_token',
                  'date_created',
                  'date_modified')
        read_only_fields = ('date_created', 'date_modified', 'profile_pic')
from rest_framework import generics
from .serializers import UserSerializer
from .models import User


class CreateUser(generics.ListCreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    def perform_create(self, serializer):
        serializer.save()

Traceback

8
  • Please paste your code itself (not images) into your question. Commented May 20, 2018 at 16:12
  • Your second and third image are the same by the way. Commented May 20, 2018 at 16:13
  • I have changed images with code. Please check now. Commented May 20, 2018 at 16:15
  • Perhaps also useful: the traceback. Commented May 20, 2018 at 16:16
  • 5
    match_price = models.IntegerField <= is this a typo here or in your code? Needs to be match_price = models.IntegerField(). Commented May 20, 2018 at 16:18

1 Answer 1

1

This code will work..

class CreateUser(generics.ListCreateAPIView):
    serializer_class = UserSerializer
    queryset = User.objects.all()

    def create(self, request, *args, **kwargs): 
       serializer = UserSerializer(data=request.data)
       if not serializer.is_valid(raise_exception=False):
          return Response({"status":"failure","status_message":serializer.errors}, status=status.HTTP_400_BAD_REQUEST)

       serializer.save()
       return Response({"status":"success","status_message":"User Created Successfully"}, status=status.HTTP_200_OK)
Sign up to request clarification or add additional context in comments.

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.