Can I override the create method of DatatablesEditorModelViewSet?
Can I override the create method of DatatablesEditorModelViewSet?
Description of problem:
Hi there! I'm new to Django, DRF, DataTables and DataTables Editor.
I'd like to add a row to a dataset that I've rendered using DataTables with a Python backend. The create method from the editor extension will call the view, which extends DatatablesEditorModelViewSet. I'd like to intercept the form submission, and add some information about the user making the request, as well as the time and send to that database as well.
In DRF I think the pattern would be something like this:
class AnObjectsViewSet(PermissionRequiredMixin, DatatablesEditorModelViewSet):
raise_exception = True
permission_required = 'permission.permission'
queryset = AnObject.objects.all()
serializer_class = AnObjectSerializer
filter_fields = '__all__'
def create(self, request):
logger = logging.getLogger(__name__)
request_dict = request.data
request_keys = list(request_dict.keys())
logger.info(request_keys)
# I'd like to set a few values in the form data, such as the update user and update time,
# then pass the object along.
return JsonResponse({'data': None}, status=HTTP_201_CREATED)
I don't believe this question has been asked before, and I didn't see an example of this on the site or discussed in the documentation.
Thank you very much for your time. I'd be happy to provide any additional information to help answer the question.
Answers
Sounds like what you want to do is something outside the scope for Datatables and Editor. For Python and Django specific questions you can use something like Stack Overflow.
Sounds like you are interested in adding data to
request_dict
. Not sure where the update user information would come from. You can usedatetime.datetime.now()
to get the current time.If you ware creating your own Editor backend you will need to follow the Editor protocol to return the appropriate response. In the case of create you should fetch the created record from the DB then return that to the client.
Presumable this is from a third party library you downloaded? If so you will need to contact the developer for support of that package. Datatables has not provided any Python based server scripts.
Kevin