1234567891011121314151617181920212223242526272829 |
- from rest_framework import serializers
- from .models import User
- class UserModelSerializer(serializers.ModelSerializer):
- # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
- class Meta:
- # 1,参考BookInfo,生成序列化器的字段
- model = User
- fields = '__all__'
- # 2、fields指定生成字段
- # fields = ['id','password','is_superuser','username']
- # 3、read_only_fields指定只读字段
- # read_only_fields = ["bpub_date", "bread", "bcomment"]
- extra_kwargs = {
- "groups": {
- "read_only": True
- },
- "user_permissions": {
- "read_only": True
- }
- }
- def create(self, validated_data):
- '''重写create方法实现,将密码加密后保存'''
- return User.objects.create_user(**validated_data)
|