0

I have a requirement where users will be selecting a few rows from the front end table. (In my case it is React and Antd is the UI library).

When the user selects a row or rows from the frontend, those row information will be passed from frontend to the backend through ajax call, and those rows should be locked in the backend so that no other user can access the selected rows. If the rows are selected at the same time by some other user then a warning response should be sent to the user. For the backend, I'm using Django Rest Framework. I'm somewhat familiar with Django and Django Rest Framework on function-based view. For locking a row I know I've to use select_for_update method but I'm not sure how to implement it for multiple rows.

Can anyone please give me backend(i.e. Django Rest Framework) code example how to achieve this.

Aashay Amballi
  • 1,321
  • 3
  • 17
  • 39
  • 1
    Possible duplicate of [select\_for\_update in development Django](https://stackoverflow.com/questions/17149587/select-for-update-in-development-django) – Brown Bear Sep 23 '19 at 07:04
  • Yeah, I will look into it :) – Aashay Amballi Sep 23 '19 at 07:17
  • This is probably something you should implement at your application level as `select_for_update()` only works within one transaction so you cannot lock it for another request. You should probably have a state field in the model and with `locked` and `unlocked` values and use permissions to prevent any operation on a locked row – Ken4scholars Sep 23 '19 at 07:38
  • Yeah, I was thinking to have a field named chosen with the BooleanField. If the row is selected then it will be true if not then false. – Aashay Amballi Sep 23 '19 at 07:56
  • I'd say have the user initiate a "fake" transaction in a new table, and then "lock" the rows with that tx_id when selecting. Then you can clear the field when a tx times out and in theory show the other user who has actually locked the records. – Andrew Sep 23 '19 at 13:15

0 Answers0