Django中TypeError: Object of type function is not JSON serializable错误及GET请求参数返回Null问题求助
Hey there! Let's work through the two issues you've hit with your Django REST Framework API, step by step.
First: Recapping the Fixed TypeError
You already nailed this one, but just to clarify for context: your original code returned {'result': result}—but result here was the name of your view function itself, not the results variable you created with your solute/solvent values. Swapping result for results fixed the serialization error, since you’re now passing the list variable instead of a function object.
Second: Fixing the Null Response (Retrieving URL Parameters Correctly)
The reason you’re getting null values now is because you’re using request.data to fetch parameters from a GET request—and that’s not where GET parameters live.
In DRF, request.data is meant for data sent in the request body (like POST/PUT payloads). For parameters passed via the URL’s query string (the ?key=value section), you need to use request.query_params—it’s DRF’s recommended wrapper around Django’s native request.GET object.
Here’s your updated, working code:
@api_view(['GET']) def result(request): # Fetch GET parameters from query_params instead of request.data solute = request.query_params.get('solute', None) solvent = request.query_params.get('solvent', None) results = [solute, solvent] return Response({'result': results}, status=200)
How this works:
When you visit http://127.0.0.1:8000/predict/?solute=CC(C)(C)Br&solvent=CC(C)(C)O, DRF parses the query string into the request.query_params dictionary. Using .get() lets you safely retrieve the values, with None as a fallback if a parameter is missing.
Now when you hit that URL, your API should return the expected response:
{"result": ["CC(C)(C)Br", "CC(C)(C)O"]}
内容的提问来源于stack exchange,提问作者harsh

