I am new to django-rest-framework, so I'm sorry if my question is too simple or doesn't make much sense.
I have programmed a scientific python package and would like to make it accessible through the API. For this, I use the django rest framework.
From a high level point of view, in order to make one specific function inside my package, I have to configure two files. In the serializer file, I define a specific check that converts the incoming request to the native python types and defines a check for input parameters. In the view file, I call validation and do some conversions before calling the package's internal function.
Suppose my function that I want to open through the API is as follows:
In [6]: def f(a):
...: return np.mean(a)
...:
Now the API will receive input through a json file. My first question is: how to define a serializer for the function above? Is ListField the right choice?
class fSerializer(serializers.Serializer):
nparray = serializers.ListField(
source="a",
child=serializers.DecimalField(max_digits=12, decimal_places=2)
)
Or are there other, more suitable fields?
The view file will look something like this:
class fViewSet(viewsets.ViewSet):
def create(self, request):
serializer = fSerializer(data=request.data)
if serializer.is_valid():
try:
a = np.asarray(serializer.validated_data.get("a"))
json_return = json.dumps(f(a))
return HttpResponse(json_return,
content_type='application/json')
As you can see, I first convert the verified data to a numpy arary. But is this the correct (pythonic) way to do this? Should this be done in the serializer?
If someone has a tutorial on how to open this function through the API, they are more than happy to read it.