Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

authentication serializer drf

from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.db import models
from django.db.models import fields
from rest_framework import serializers

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'email')


class RegisterSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'password')
        extra_kwargs = {'password': {'write_only': True}}

    def create(self, validated_data):
        user = User.objects.create_user(validated_data['username'], validated_data['email'], validated_data['password'])
        return user


class LoginSerializer(serializers.Serializer):
    username = serializers.CharField()
    password = serializers.CharField()

    def validate(self, data):
        user = authenticate(**data)
        if user and user.is_active:
            return user
        raise serializers.ValidationError('Incorrect Credentials Passed.')
Comment

PREVIOUS NEXT
Code Example
Python :: tuple push 
Python :: cv2.imshow not working in vscode 
Python :: Common Python String Methods 
Python :: generative art python 
Python :: regex find all sentences python 
Python :: select column in pandas dataframe 
Python :: python change label text 
Python :: how can I convert dataframe to list with in python without changing its datatype? 
Python :: python re split 
Python :: Get text without inner tags text in selenium 
Python :: #Function in python 
Python :: Python remove duplicate lines from a text file 
Python :: python list pop 
Python :: Maximize Difference codechef solution 
Python :: python how to invert an array 
Python :: target encoder sklearn example 
Python :: python coding practice 
Python :: python how to iterate through a list of lists 
Python :: flask add_url_rule 
Python :: dimension of an indez pandas 
Python :: Split a list based on a condition 
Python :: create QAction with icon in pyqt 
Python :: scrape website with login python selenium 
Python :: chat application in python 
Python :: add columns not in place 
Python :: python web app 
Python :: FileSystemStorage django 
Python :: how to create a subset of two columns in a dataframe 
Python :: zip function python 
Python :: python hash 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =