I would like to know what is the way for the Response of a POST with rest_framework_bulk, to be modifiable since by default it always returns the same object or array of object that is sent to it by the service.
model:
class employee():
name = models.CharField(max_length=4, verbose_name='name')
profession = models.CharField(max_length=4, verbose_name='profession')
userCreation = models.ForeignKey(userCreation, verbose_name='user')
class Meta:
verbose_name='employee'
def __unicode__(self):
return self.name
serializers:
from django.forms import widgets
from .models import employee
from rest_framework import serializers
from rest_framework_bulk import (BulkListSerializer, BulkSerializerMixin, ListBulkCreateUpdateDestroyAPIView,)
class employeeSerializer(BulkSerializerMixin, serializers.ModelSerializer):
class Meta(object):
relation_user = serializers.ReadOnlyField(source='user.username')
model = employee
fields = ('id', 'name', 'profession', 'relation_user')
view:
from django.shortcuts import render
from .models import employee
from .serializers import employeeSerializer
from rest_framework import generics
from rest_framework_bulk import ListBulkCreateAPIView
class createEmployee(ListBulkCreateAPIView):
queryset = employee.objects.all()
serializer_class = employeeSerializer
def perform_create(self, serializer):
serializer.save(relation_user=self.request.user)
url:
from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
from employee import views
urlpatterns = [url(r'^api/createEmployee/$', views.createEmployee.as_view())]
urlpatterns = format_suffix_patterns(urlpatterns)
def perform_create(self, serializer):
serializer.save(relation_user=self.request.user)
This works very well and creates the employee without problems, Json who sent the rest service
{
name: 'Jhon',
profession: 'Medic'
}
and this is the answer:
{
name: 'Jhon',
profession: 'Medic'
}
I would like that in the Response something different could be placed as a sum, a calculation or simply a message that says created employee.
I think with an APIView it would be something like
return Response(succes='Empleado creado con exito')