I am in the process of rewriting the backend of an internal website from PHP to Django (using REST framework).
Both versions (PHP and Django) need to be deployed concurrently for a while, and we have a set of software tools that interact with the legacy website through a simple AJAX API. All requests are done with the GET method.
My approach so far to make requests work on both sites was to make a simple adapter app, routed to 'http://<site-name>/ajax.php
' to simulate the call to the Ajax controller. Said app contains one simple function based view which retrieves data from the incoming request to determine which corresponding Django view to call on the incoming request (basically what the Ajax controller does on the PHP version).
It does work, but I encountered a problem. One of my API actions was a simple entry creation in a DB table. So I defined my DRF viewset using some generic mixins:
class MyViewSet(MyGenericViewSet, CreateModelMixin):
# ...
This adds a create
action routed to POST
requests on the page. Exactly what I need. Except my incoming requests are using GET
method... I could write my own create
action and make it accept GET
requests, but in the long run, our tools will adapt to the Django API and the adapter app will no longer be needed so I would rather have "clean" view sets and models. It makes more sense to use POST
for such an action.
In my adapter app view, I naively tried this:
request.method = "POST"
request.POST = request.GET
Before handing the request to the create
view. As expected it did not work and I got a CSRF authentication failure message, although my adapter app view has a @csrf_exempt
decorator...
I know I might be trying to fit triangle in squares here, but is there a way to make this work without rewriting my own create
action ?
from Convert GET parameters to POST data on a Request object in Django REST Framework
No comments:
Post a Comment