dlewin
September 28, 2021, 5:57pm
1
I’m writing a function from a view that call a serializer (from DRF) as below.
The purpose of this, is simply to call this function by providing any class that I’ll add further in the serializers.py.
def get_list( class_seralizer):
eui_value = request.query_params.get('eui', None)
returned_object = returned_object.filter(eui__contains=eui_value)
ret_serializer = class_seralizer(returned_object, many=True) #here
print( type(ret_serializer))
return JsonResponse(ret_serializer.data, safe=False)
the print displays:
class ‘rest_framework.serializers.ListSerializer’
and the serializers.py :
class class_seralizer(serializers.ModelSerializer):
class Meta:
model = CpuConf
fields = (
'id',
'eui',
'hostname',
'ip_address',
'subnet',
'dns',
'ntp_address')
But this ends with :
Exception Type:
AssertionError
Exception Value:
Expected a Response
, HttpResponse
or HttpStreamingResponse
to be returned from the view, but received a <class 'NoneType'>
It’s complaining about your view - not the code you’ve posted here.
Your view is not returning a Response
as the messages says.
dlewin
September 29, 2021, 9:02am
3
This code is from the view itself.
To be more specific :
I’ve changed existing working code :
@api_view(['GET', 'POST', 'DELETE'])
def cpuconf_list(request):
if request.method == 'GET':
returned_object = query_all(module)
eui_value = request.query_params.get('eui', None) # Check if the UID exists
if eui_value is not None:
returned_object = returned_object.filter(eui__contains=eui_value)
confs_serializer = CpuConfSerializer(returned_object, many=True)
print("===", type(confs_serializer))
return JsonResponse(confs_serializer.data, safe=False)
to the code :
@api_view(['GET', 'POST', 'DELETE'])
def cpuconf_list(request):
conf_list(request, CpuConf, CpuConfSerializer)
def conf_list( request,module, class_seralizer):
eui_value = request.query_params.get('eui', None)
returned_object = returned_object.filter(eui__contains=eui_value)
ret_serializer = class_seralizer(returned_object, many=True) #here
print( type(ret_serializer))
return JsonResponse(ret_serializer.data, safe=False)
So the CpuConfSerializer call then become generic enough to be replaced by any other class by using “class_serializer”
dlewin
September 29, 2021, 10:32am
4
oh just understood the consequences of your answer : I’ve removed the return from the call function itself.
Thanks