The cleanest, most robust approach is to use a DB-side sequence (PostgreSQL’s CREATE SEQUENCE
or the django-sequences package) so that each order_sl
is allocated atomically by the database itself. If you cannot add a sequence, wrap your max-lookup and insert in a single transaction with select_for_update()
.