I want to create a view using APIView, which shows all the messages sent by a user specified in the url.
models.py:
class Message(models.Model):
body = models.CharField(max_length=500)
normaluser = models.ForeignKey(User, on_delete=models.CASCADE)
As User I am using the User table provided by Django.
views.py:
class MessageUserView(APIView):
def get(self, request, pk): # devolve objecto
normaluser = MessageSerializer(instance=Message.objects.get(normaluser),
many=False)
serializer = MessageSerializer(instance=Message.objects.filter(pk=normaluser),
many=True)
return Response(serializer.data)
At this point when running the code gives me the following error:
UnboundLocalError at / website / message-user / 2 local variable 'normaluser' referenced before assignment