2022-10-25 23:29:13 +03:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
2022-10-26 23:22:04 +03:00
|
|
|
fields = ["username", "url"]
|
2022-10-25 23:29:13 +03:00
|
|
|
|
|
|
|
extra_kwargs = {
|
|
|
|
"url": {"view_name": "api:user-detail", "lookup_field": "username"}
|
|
|
|
}
|
2022-10-26 23:22:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
class RegisterSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = (
|
|
|
|
"username",
|
|
|
|
"password",
|
|
|
|
)
|
|
|
|
extra_kwargs = {"password": {"write_only": True}}
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
user = User.objects.create(
|
|
|
|
username=validated_data["username"],
|
|
|
|
)
|
|
|
|
|
|
|
|
user.set_password(validated_data["password"])
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
return user
|