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 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@ShreehariVaasisthaL I am not sure if you can do it with bulk_update, probably you will need to validate it by yourself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.