一个让人很郁闷的问题 DRF

今天在在处理一个嵌套结构API 的时候遇到一个很无语的问题: 开始我这样写,是因为 username 只有一个

   def get_host(self,obj):

    h = User.objects.filter( username = 'wow')
    result = HostSerializer(h)
    return result.data

注意,他的报错信息:

Got AttributeError when attempting to get a value for field `username` on serializer `HostSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance.
Original exception text was: 'QuerySet' object has no attribute 'username'.

强制加上 many = True 就好了!没想到只有一个也需要加上 many

    def get_host(self,obj):

    h = User.objects.filter( username = 'wow')
    result = HostSerializer(h,many=True)
    return result.data