I recently followed the Django API tutorial. http://www.django-rest-framework.org/tutorial/6-viewsets-and-routers/

When I initiate a POST request my intent is that after inserting the record into database, launch a process. Example:

POST server/

Will insert a new "server" record, return 201 to API client and behind the scenes I want to launch a process that will install and discover the server via some ssh/icmp. Where is the best place to put this code any tutorial or advise? How can I return a 201 and after that execute my discovery process. Is it better to monitor the db for new records and have a different process doing it?

def snippet_list(request):
    List all code snippets, or create a new snippet.
    if request.method == 'GET':
        snippets = Snippet.objects.all()
        serializer = SnippetSerializer(snippets, many=True)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(data=data)
        if serializer.is_valid():
            <? is here?>
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400)

If you only need to do this on the creation of a record, why not call the method as part of the view. Something like this.

Server_form = ServerForm(request.POST)
if server_form.is_valid():
    profile = server_form.save(commit=False) 
    profile.user = request.user
   #insert your code here to call your method


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.