我正在使用具有用户配置文件的自定义用户模型,该用户配置文件使用OneToOneField
以下结构:
email
password
profile : {
username
avatar
}
它对移动设备非常有效,因为我基本上只是来回发送 json,但是每当我将我的 API 与网站一起使用时,这都是一个问题,因为 html 中无法发送类似 json 的数据,因此我无法填写“个人资料”字段不使用 ajax。
我希望有可能使用纯 html 表单并将 javascript 仅用于客户端验证和动画,而不将其用于注册等评论功能。
我也可以使用以下结构,然后为配置文件服务器端重建一个字典,但我确实觉得它不够通用。
email
password
username
avatar
用户序列化器.py
class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
profile = ProfileSerializer(required=True)
class Meta:
model = get_user_model()
fields = ('id', 'email', 'password', 'date_joined', 'profile')
def create(self, validated_data):
#profile_data = validated_data.pop('profile')
password = validated_data.pop('password')
user = get_user_model().objects.create_user(**validated_data)
user.set_password(password)
#user.save()
print(user)
#print(profile_data
ProfileSerializer.py
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('username',)
因此,无论我使用的是 json 数据还是表单数据,显然它都在等待配置文件(对象)数据。我希望能够使用 html 表单来做到这一点。
知道我应该如何处理吗?