我想管理员发布名称,电子邮件和使用前端的用户的角色,其存储在后端的django,请注意,我不希望管理员存储密码,用户将稍后添加它.下面的代码是给错误400当我输入姓名,电子邮件和角色
djangocode:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = UserAccount
fields = ['email', 'name', 'username', 'password', 'role']
def create(self, validated_data):
password = validated_data('password')
username = validated_data('username')
user = UserAccount(
email=validated_data['email'],
role=validated_data['role'],
name=validated_data['name']
)
if username is not None:
username=validated_data['username'],
if password is not None:
user.set_password(validated_data['password'])
user.save()
return user
React代码:
const handleSubmit = (event) => {
event.preventDefault();
const data = {
name : name,
email : email,
role: role
};
console.log("data is "+data);
axios.post('http://127.0.0.1:8000/api/register/', data)
.then(response => console.log("got it"+response.data))
.catch(error => console.log(error));
console.log('name:', name);
console.log('email:', email);
console.log('role:', role);
1条答案
按热度按时间jdg4fx2g1#
给予这个:
在这里,我们根据用户角色有条件地使字段成为必填字段。