nowait or another method in Django admin page

38 views Asked by At

I'm using Django and trying to find something similar to nowait=True in Django admin page. I have some script which locks entries(select for update) during working. So, when I try to change some fields values in Django admin page for that entry when its locked, admin page waits for unlocking that entry(in my case it could take minutes). I want to raise error(something like "database entry is locked") in admin page without waiting for unlocking.

1

There are 1 answers

3
Goroshek On

Have found solution. We can overwrite save_model method

class SomeDBAdmin(admin.ModelAdmin):
    list_display = ('id', 'name')

    @transaction.atomic
    def save_model(self, request, obj, form, change):
        try:
            SomeDB.objects.select_for_update(nowait=True).get_or_create(id=obj.id)
        except DatabaseError as e:
        # if new record in db save it.
            if not change:
                obj.save()
            print(f'exception {e}')

        super().save_model(request, obj, form, change)