Implementation of REST Based on mongo

Keywords: REST Django github MySQL

With the widespread use of NoSQL, the use of traditional relational data such as mysql is gradually decreasing.
django's REST support for mysql should be very clear to all, but mongo's support does not seem to be so perfect, it still needs to be supplemented by itself, which is always not easy to use.
The company just had a project to add, delete and modify Mongo data. I took this opportunity to write a class to implement Mongo REST. This class is based on Django View (from django.views.generic import View). It does not refer to other libraries. It is also very convenient to use. Here is a simple display of the code (the following code is incomplete, you need to download the code to github, https://github.com). / guozhihua12/mongo-rest:

First, MongoListView accepts get and post requests. Get corresponds to list interface and post is create interface.
One feature of Mongo is that you don't know which columns there are. If you use dynamic documnets, we need to do to_mongo().to_dict() for all the values. There is also the need for res = self.document(**results) when post ing to save all incoming values.
class MongoListView(View):
    document = None
    queryset = None
    initial = {}

    def get(self, request, *args, **kwargs):
        self.object_list = self.get_queryset()
        result = []
        for row in self.object_list:
            value = row.to_mongo().to_dict()
            value.pop('_id')
            result.append(value)
        return JsonResponse(Results().succss_result(result))

    def post(self, request, *args, **kwargs):
        self.object = None
        results = self.get_form_kwargs().get('data').dict()
        res = self.document(**results)
        res.save()
        return HttpResponseRedirect(res.get_absolute_url())

Here is MongoObjectView, which I inherited from MongoListView to avoid code duplication. This class accepts three requests -- get, post and delete (corresponding to detail, update, delete operations, respectively). Emphasis will be placed on the update operation. Because of the uncertainty of the Mongo column, we need to take out the old_results = self.object.to_mongo().to_dict(), and then transform the incoming data into dict, new_results = self.get_form_kwargs().get('data'). dict(), and then combine old_ress and new_results into a new dict to save in mongo.

class MongoObjectView(MongoListView):
    document = None
    slug_url_kwarg = 'slug'
    pk_url_kwarg = 'pk'
    slug_field = 'slug'

    def get(self, request, **kwargs):
        self.object = self.get_object()
        result = self.object.to_mongo().to_dict()
        result.pop('_id')
        return JsonResponse(Results().succss_result(result))

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        old_results = self.object.to_mongo().to_dict()
        new_results = self.get_form_kwargs().get('data').dict()
        results=dict(old_results.items()+new_results.items())
        res = self.document(**results)
        res.save()
        self.object.delete()
        return HttpResponseRedirect(res.get_absolute_url())


    def delete(self, request, *args, **kwargs):
        self.object = self.get_object()
        self.object.delete()
        return JsonResponse(Results().succss_result())

Complete code: https://github.com/guozhihua12/mongo-rest

Posted by Maleko on Sat, 30 Mar 2019 15:54:30 -0700