serializers.py
1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from rest_framework import serializers
from api.models import User
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
class SignUpSerializer(serializers.ModelSerializer):
password2 = serializers.CharField(style={"input_type": "password"})
class Meta:
model = User
fields = ['user_id', 'name', 'password', 'password2']
def create(self, data):
user_id = data['user_id']
name = data['name']
password = self.data['password']
password2 = self.data['password2']
if user_id and User.objects.filter(user_id=user_id).exclude(name=name).exists():
raise serializers.ValidationError({"user_id": "User_id must be unique."})
elif password != password2:
raise serializers.ValidationError({'password': "Passwords must match."})
user = User.objects.create(
user_id=data['user_id'],
name=data['name'],
password=data['password'],
)
user.save()
return user
class UserInfoSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['user_id', 'name', 'total_size', 'current_size', 'created_time']