0

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.

1 Answers1

0

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

Ersain
  • 1,466
  • 1
  • 9
  • 20
  • `bulk_update()` does not throw any error if the row does not exist, is there any way to raise the error? So that it can be within `transaction.atomic()` block and I will be able to throw errors accordingly. – Shreehari Vaasistha L Jun 27 '22 at 09:51
  • @ShreehariVaasisthaL I am not sure if you can do it with `bulk_update`, probably you will need to validate it by yourself – Ersain Jun 28 '22 at 05:32