1 回答
TA贡献1872条经验 获得超3个赞
查看您的其他问题,似乎您 user = UserSerializer(read_only=True)只需要 GET 请求。如果是这样,
class CompanySerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
try:
if self.context['request'].method in ['GET']:
self.fields['members'] = serializers.SerializerMethodField()
self.fields['user'] = UserSerializer()
except KeyError:
pass
class Meta:
model = Company
fields = '__all__'
def get_members(self, obj):
accounts = Account.objects.filter(id__in=obj.members)
return AccountSerializer(accounts, many=True).data
添加回答
举报