I have the following User
,
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True, max_length=255)
username = models.CharField(null=False, unique=True, max_length=255)
full_name = models.CharField(max_length=255, blank=True, null=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
以下是UserProfile
模型:
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, )
level = models.CharField(default="Noob", max_length=255)
reputation = models.IntegerField(default=0)
status = models.CharField(max_length=255, null=True, blank=True)
用户与个人资料Profile
之间是一对一的关系。
这是UserSerializer
。
class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
location = LocationSerializer(read_only=True)
profile = UserProfileSerializer(read_only=True)
class Meta:
model = models.User
fields = (
'id', 'email', 'mobile', 'username', 'full_name', 'password', 'is_active', 'profile',
)
这是个人资料序列化器。
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = models.UserProfile
fields = ('level', 'reputation', 'status',)
问题在于用户的序列化输出中没有嵌套的个人资料数据。我该如何解决这个问题?感谢您的任何帮助。
fields
包含一个空字符串吗? - Willem Van Onsem