我正在使用Django rest-framework,我使用API来向用户个人资料注册新用户,但是我收到此错误,不仅是关于字段,而且是用户个人资料模型中的其他字段,例如体重和身高,我包含了代码
这是我的模型。py
class Meta:
managed = True
db_table = 'user'
class UserProfile(models.Model):
weight = models.FloatField()
height = models.FloatField()
about = models.TextField(blank=True, null=True)
license_number = models.IntegerField(unique=True)
created = models.DateField(blank=True, null=True)
updated = models.DateField(blank=True, null=True)
#id = models.AutoField(unique=True)
user_id = models.ForeignKey(User, models.DO_NOTHING)
class Meta:
managed = False
db_table = 'user_profile'
there are the serializers classes :
class UserprofileSerializer(serializers.ModelSerializer):
class Meta :
model = UserProfile
fields =('weight','height','about','license_number','created','updated','id','user_id')
class UserSerializer(serializers.HyperlinkedModelSerializer):
profile = UserprofileSerializer(required = True)
class Meta :
model = User
fields = ('id','first_name','last_name','email','password','user_type','created','updated','profile')
extra_kwargs = {'password': {'write_only': True}}
def create(self,validated_data):
profile_data = validated_data.pop('profile')
password = validated_data.pop('password')
user = User(**validated_data)
user.set_password(password)
user.save()
User.objects.create(user=user,**profile_data)
return user