Home > front end >  Efficient way to re-order objects in Django
Efficient way to re-order objects in Django

Time:06-28

I have written a query to re-order objects based on the values I get in the API request. Below is the sample code I have written:

    @action(detail=False, permission_classes=[], methods=["PUT"])
    def reorder_modules(self, request, *args, **kwargs):
        """
        ReOrdering Modules
        Sample request data
        {
            <module_id>: <ordering_number>
            12: 2,
            13: 1,
            14, 3,
        }
        """
        updatabale_modules = []
        for module_id, module_order in request.data.get("modules", {}).items():
            _mod = Module.objects.get(id=module_id)
            _mod.order = module_order
            updatabale_modules.append(_mod)
        Module.objects.bulk_update(updatabale_modules, ["order"])
        return Response({"detail": "successfully reordered course modules"}, status=HTTP_200_OK)

Is there any way to avoid looping and getting each module? If that's possible I should be able to save making a few queries.

FYI, I'm using MySQL database.

CodePudding user response:

In stead of querying directly Module.objects.get(id=module_id), you can instantiate a Module object using: Module(id=model_id), so that rewrite your method to:

def reorder_modules(self, request, *args, **kwargs):
    updatabale_modules = []
    for module_id, module_order in request.data.get("modules", {}).items():
        _mod = Module(id=module_id)
        _mod.order = module_order
        updatabale_modules.append(_mod)
    Module.objects.bulk_update(updatabale_modules, ["order"])
    return Response({"detail": "successfully reordered course modules"}, status=HTTP_200_OK)

I debugged this view with query inspector and it really produced only one db query, and it works with non-existent ids

  • Related