Skip to content

Instantly share code, notes, and snippets.

@tmirza-dinCloud
Created June 13, 2020 16:18
Show Gist options
  • Save tmirza-dinCloud/1313c006eb238483e3310d65dcadb507 to your computer and use it in GitHub Desktop.
Save tmirza-dinCloud/1313c006eb238483e3310d65dcadb507 to your computer and use it in GitHub Desktop.
from demo.models import User
from rest_framework import serializers
import pyotp
class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'password', 'mfa_hash')
def create(self, validated_data):
user = User.objects.create(**validated_data)
user.set_password(validated_data['password'])
user.mfa_hash = pyotp.random_base32()
user.save()
return user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment