Create a Modelform which can only be submitted by 1 person at a time?

Hi All,

I am facing a challenge and giving a brief intro for better understanding of the problem:

I am working on a functionality, where a DB table is having a field status with valid choices as assigned and un-assigned .

I have used ListView class to display the model data as a table and added a column which has a button called “Assign”.
image

On clicking on assign an Assignment form will open up and they can submit. Once this form is submitted the status will change to assigned and the assignment button will disappear. All this is fine and done already.

  1. Now the challenge is to design such that whichever user clicks on assign first will get the option to assign it only and any consecutive users who try to click on assign will not be allowed to assign.

  2. Also there has to be check in the “Assignment form” submit (POST method) view to check if that process status is already updated to “Assigned”.

This problem mainly arises when 2 users have opened the page some time back and without refreshing the page they are trying to click on “Assign” button.

It would be life saving if someone can help me out with this.

In your view that handles the button press, check the status of the object before assigning it. If it’s already assigned, do what you need to do in that situation.

In the simple case, this is going to cover 99+% of what’s going to happen. If you need it to be even more precise, use the select_for_update function to retrieve the row to be tested.

We can’t be more specific without seeing the view being used here.

Thanks for the help.
Let me read on this somewhat and then get back to you if required.

I implemented the select_for_update function and transaction.atomic.

But how do I test it if its working as expected?