我是django的新手,并开始编写序列化程序并附带此问题
我已经从user_profile类
扩展了我的用户类现在我想编写user_profile序列化程序以在用户配置文件和用户
中保存数据这是我的用户序列化程序
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email','password','is_staff')
它工作正常
这是我的user_profile类
class User_Profile(models.Model):
class Meta:
db_table = 'user_profile'
user = models.OneToOneField(User, on_delete=models.CASCADE)
phoneNumber = models.CharField(max_length=10)
现在我为用户个人资料编写此序列化程序
class UserProfileSerializer(serializers.HyperlinkedModelSerializer):
email = serializers.CharField(source='user.email')
username = serializers.CharField(source='user.username')
class Meta:
model = User_Profile
fields = ('url', 'phoneNumber','email','username')
但它会保存用户和用户个人资料数据,并且会出现错误
Cannot assign "{u'username': u'test12', u'email': u'test@12.cm'}": "User_Profile.user" must be a "User" instance.
请帮忙!