하재연

Update serializers.py

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