0
我有這個序列化程序和模型。我想在我的API中顯示用戶名的用戶模型,但我收到此錯誤。Django Rest框架slug_field錯誤
AttributeError at /api/v1/client_share_requests/1/
'Profile' object has no attribute 'username'
serializers.py
class ClientShareRequestSerializer(serializers.ModelSerializer):
checked_by = serializers.SlugRelatedField(
many=True,
queryset=Profile.objects.all(),
slug_field='username'
)
class Meta:
model = ClientShareRequest
fields = ('checked_by')
models.py
class Profile(models.Model):
user = models.OneToOneField(User, related_name='profile')
class ClientShareRequest(models.Model):
checked_by = models.ManyToManyField(Profile, blank=True,
related_name='checked_by')
我試圖訪問用戶模型實例是這樣的:
checked_by = serializers.SlugRelatedField(
many=True,
queryset=Profile.objects.all(),
slug_field='user.username'
)
但我收到此錯誤:
'Profile' object has no attribute 'user.username'
感謝您的幫助。 (對不起,我的英文:P)
嘗試'user__username'。 –
@RahulGupta不起作用''Profile'對象沒有屬性'user__username'' – FACode