我有以下序列化器:
select display_name, SUBSTRING_INDEX(ExtractValue(attributes,'/attributes/map/departmentAdminEmail'),' ',1) from table1 where display_name like 'John%';
用户扩展了UserProfile模型:
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ['profile_pic']
class UserSerializer(serializers.HyperlinkedModelSerializer):
userprofile = ProfileSerializer()
class Meta:
model = User
fields = ('id', 'username', 'email', 'password', 'is_superuser','userprofile')
extra_kwargs = {'password': {'write_only': True, 'required': True},
'is_superuser': {'read_only': True, 'required': False}}
def create(self, validated_data):
user = User.objects.create_user(**validated_data)
# UserProfile.objects.create(author = user)
# Token.objects.create(user=user)
return user
所以,目前我得到以下数据:
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_pic = models.ImageField(null=True, blank=True)
def __str__(self):
return self.name
但是现在我想要这样与主要用户使用同一级别的API:
{
"id": 11,
"username": "Arman1",
"email": "bsai@gmail.com",
"is_superuser": false,
"userprofile": {
"profile_pic": null
}
}
请帮助我做到这一点。预先感谢!
PS我尝试执行以下操作:
{
"id": 11,
"username": "Arman1",
"email": "bsai@gmail.com",
"is_superuser": false,
"profile_pic": null
}
但是,它返回的'NoneType'对象没有属性'items'