serializers.py 895 B

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