my views.py file:
from rest_framework.views import APIView
from rest_framework.response import Response
from .serializers import UserSerializer
class TestView(APIView):
def get(self, request, format=None):
print("API called")
return Response("You did it!", status=200)
class UserView(APIView):
def post(self, request, format=None):
print("User created")
user_data = request.data
print(request.data)
user_serializer = UserSerializer(data=user_data)
if user_serializer.is_valid(raise_exception=False):
user_serializer.save()
return Response({'user': user_data}, status=200)
return Response({'msg': "error: no user created"}, status=400)
my serializers.py file:
from rest_framework import serializers
from django.contrib.auth.models import User
from rest_framework.validators import UniqueValidator
from rest_framework.settings import api_settings
class UserSerializer(serializers.ModelSerializer):
token = serializers.SerializerMethodField()
email = serializers.EmailField(
required=True,
validators=[UniqueValidator(queryset=User.objects.all())]
)
username = serializers.CharField(
required=True,
max_length=32,
validators=[UniqueValidator(queryset=User.objects.all())]
)
first_name = serializers.CharField(
required=True,
max_length=32
)
last_name = serializers.CharField(
required=True,
max_length=32
)
password = serializers.CharField(
required=True,
min_length=8,
write_only=True
)
def create(self, validated_data):
password = validated_data.pop(password, None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
def get_token(self, obj):
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
payload = jwt_payload_handler(obj)
token = jwt_encode_handler(payload)
return token
class Meta:
model = User
fields = (
'token'
'username',
'password',
'first_name',
'last_name',
'email',
'id'
)
my urls.py file:
from django.urls import path
from .views import TestView, UserView
from rest_framework_jwt.views import refresh_jwt_token, verify_jwt_token
urlpatterns = [
path('test', TestView.as_view()),
path('user-view/', UserView.as_view()),
]
Postman request:
as you can see, i'm sending a username here. I know it will give error because I'm not sending the complete data with all the fields, however the error i do get is completely different
and then this is my cmd:
You can see the server runs without errors, but I keep getting an assertion error even though the class Meta is there. Please help me resolve the error!
P.S. I don't know if it matters but the frontend is on react-native and is unintegrated at the moment.
CodePudding user response:
I think you missed the indentation in the definition of the UserSerializer
.
class UserSerializer(serializers.ModelSerializer):
...
# here you need to indent so Meta belongs to the serializer.
class Meta:
model = User
fields = (
'token'
'username',
'password',
'first_name',
'last_name',
'email',
'id'
)
Hope it could help.