분명 Serializer에 `id` 필드를 명시했다!

AttributeError at /users
Got AttributeError when attempting to get a value for field `id` on serializer `UserSerializer`.
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 'id'.
Request Method:    GET
Request URL:    http://127.0.0.1:8000/users
Django Version:    3.2.7
Exception Type:    AttributeError
Exception Value:    
Got AttributeError when attempting to get a value for field `id` on serializer `UserSerializer`.
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 'id'.
  • serializer는 한 개의 객체만 이해할 수 있고 리스트는 이해할 수 없기 때문이다.
  • objects.all() 은 객체들의 리스트를 리턴하므로 many=True 옵션을 추가해줌으로써 해결
...
users = User.objects.all()
serializer = UserSerializer(users, many=True)
...

+ Recent posts