1 Star 8 Fork 8

杜赛/django-vue-tutorial

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
serializers.py 1.44 KB
Copy Edit Raw Blame History
杜赛 authored 4 years ago . p280
from django.contrib.auth.models import User
from rest_framework import serializers
class UserDescSerializer(serializers.ModelSerializer):
"""于文章列表中引用的嵌套序列化器"""
class Meta:
model = User
fields = [
'id',
'username',
'last_login',
'date_joined'
]
class UserRegisterSerializer(serializers.ModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='user-detail', lookup_field='username')
class Meta:
model = User
fields = [
'url',
'id',
'username',
'password',
'is_superuser'
]
extra_kwargs = {
'password': {'write_only': True},
'is_superuser': {'read_only': True}
}
def create(self, validated_data):
user = User.objects.create_user(**validated_data)
return user
def update(self, instance, validated_data):
if 'password' in validated_data:
password = validated_data.pop('password')
instance.set_password(password)
return super().update(instance, validated_data)
class UserDetailSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = [
'id',
'username',
'last_name',
'first_name',
'email',
'last_login',
'date_joined'
]
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/stacklens/django-vue-tutorial.git
git@gitee.com:stacklens/django-vue-tutorial.git
stacklens
django-vue-tutorial
django-vue-tutorial
master

Search