This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I want to start by saying I am fairly new to Django Rest Framework. I have a fully functioning users app built in Django which has user registration, login, logout, password change and password reset all working well. I am trying to replicate these to have their own REST API endpoints using DRF.
I have created a custom user model as shown below. I have written a serializer to handle user registration as shown below. Everything works fine except that the default password validators which are in-built in Django seem to be missing. How can I enable it as it is readily available in settings.py? Any guidance on this would be greatly appreciated.
models.py:
class CustomUser(AbstractBaseUser):
email = models.EmailField(unique=True, max_length=100)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255, blank=True, default='')
serializers.py
class UserRegisterSerializer(serializers.ModelSerializer):
# Define password fields so that text entered is not visible
password = serializers.CharField(
style={'input_type': 'password'},
write_only=True,
required=True,
)
password2 = serializers.CharField(
style={'input_type': 'password'},
write_only=True,
required=True,
)
class Meta:
model = CustomUser
# Add fields which are to be filled during registration
fields = ['id', 'email', 'first_name', 'last_name', 'password', 'password2']
def create(self, validated_data):
# Create a new user object
user = CustomUser(
email=self.validated_data['email'],
first_name=self.validated_data['first_name'],
last_name=self.validated_data['last_name'],
)
password = self.validated_data['password']
password2 = self.validated_data['password2']
# Check if passwords entered are matching. If not, raise error
if password != password2:
raise serializers.ValidationError({'password': 'Passwords must match.'})
# Hash the password before it is saved
user.set_password(password)
# Save the user object
user.save()
return user
views.py:
class UserRegisterCreateAPIView(CreateAPIView):
serializer_class = UserRegisterSerializer
Subreddit
Post Details
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/django/comm...