* 新建`serializers`
新建文件`/api/serializers.py`
```python
from rest_framework import serializers
from .models import *
class PageRolesSerializer(serializers.ModelSerializer):
class Meta:
model = PageRoles
fields = "__all__"
class UserProfileSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
class Meta:
model = UserProfile
fields = "__all__"
def create(self, validated_data):
user = super().create(validated_data)
user.set_password(validated_data['password'])
user.save()
return user
def update(self, instance, validated_data):
user = super().update(instance, validated_data)
try:
user.set_password(validated_data['password'])
user.save()
except KeyError:
pass
return user
```