我使用rest框架,我在用户emloyee.company中有字段,该字段具有字段名称。我只需要显示名称字符串,但我有dict。
class CompanySerializer(serializers.ModelSerializer):
class Meta:
model = Company
fields = ('name',)
class UserSerializer(MySerializer):
company = CompanySerializer(source='employee.company')
class Meta:
fields = (..., 'company')
我有:
{...,"company":{"name":"My company"}}
我需要:
{...,"company":"My company"}
感谢您的帮助。
答案 0 :(得分:2)
变化:
company = CompanySerializer(source='employee.company')
为:
company = serializers.ReadOnlyField(source='employee.company.name')