I have a Django web app that registers competitors for events. When someone register for an event they are logged in and complete a registration form, then in the view I check the form, do save(commit=False) and get the highest competitor-number from existing instances of competitors and for this new competitor I set the number to highest+1, then I do save() on the
On the model I have a unique-together on 'competitor' model for event and number, this ensures that the number is unique within the event.
Now, for large matches I get an issue when many registers concurrently as they end up in the save(commit=false) -- set number -- save() in the same time, and attempt to save() with same number - and this raises an IntegrityError, duplicate key as I attempt to save a new instance with same event and number as an instance that has been created during this interval.
:-(
Now, any ideas or logic for how to avoid this issue?
/ Jens