I am beginner in Django REST framework and need your advice. I am developing a web service. The service has to provide REST interface to other services. The REST interface, which I need to implement, is not working with my models directly (I mean the get, put, post, delete operations). Instead, it provides other services with some calculation results. On a request my service makes some calculations and just returns the results back (doesn't store the results in its own database).
Below is my understanding of how that REST interface could be implemented. Correct me, if I am wrong.
- Create class which makes the calculations. Name it 'CalcClass'. CalcClass uses the models in its work.
- Params necessary for the calculations are passed to the constructor.
- Implement the calc operation. It returns results as 'ResultClass'.
- Create ResultClass.
- Derived from object.
- It just only has attributes containing the calc results.
- One part of the calc results is represented as tuple of tuples. As I understand, it would be better for further serialization to implement a separate class for those results and add list of such objects to ResultClass.
- Create Serializer for ResultClass.
- Derive from serializers.Serializer.
- The calc results are read-only, so use mostly Field class for fields, instead of specialized classes, such as IntegerField.
- I should not impl save() method neither on ResultClass, nor on Serializer, because I am not going to store the results (I just want to return them on request).
- Impl serializer for nested results (remember tuple of tuples mentioned above).
- Create View to return calculation results.
- Derive from APIView.
- Need just get().
- In get() create CalcClass with params retrieved from the request, call its calc(), get ResultClass, create Serializer and pass the ResultClass to it, return Response(serializer.data).
- URLs
- There is no api root in my case. I should just have URLs to get various calc results (calc with diff params).
- Add calling format_suffix_patterns for api browsing.
Did I miss something? Is the approach is correct in general?