So i have a table call Book. I am using modelviewset but i wanted my response to have an add on response.
In my Book table :
class Book(models.Model):
book = models.CharField(max_length=10, blank=True, null=True)
author = models.CharField(max_length=10, blank=True, null=True)
serializer
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ('id', 'book', 'author')
view
class BookViewSet(viewsets.ModelViewSet):
permission_classes = [AllowAny]
queryset = Book.objects.all()
serializer_class = BookSerializer
the return result after post or update method will return the data/field which user have create/update. But i wanted to add on to it for example.
current result
{
"id": 1,
"book": "hello",
"author": "helloauth",
}
result i want
{
"id": 1,
"book": "hello",
"author": "helloauth",
"message": "You have successfully create a book",
"status": "200",
}
The custom code i have now is just showing the message only:
custom views
class BookViewSet(viewsets.ModelViewSet):
permission_classes = [AllowAny]
queryset = Book.objects.all()
serializer_class = BookSerializer
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response({"message": "You have successfully create a book",
"status": "200"}, headers=headers)
How to i make it combine/display together ?