[Fixed]-How to populate choice form from db in Django?

1👍

I would just override the delete_model as custom action and there check if the selected sector object is in use.

def delete_model(modeladmin, request, queryset):
    for obj in queryset:
        if UserModel.objects.filter(sector=obj).exists():
            # do not delete, just add some message warning the admin about it
        else:
            obj.delete()

class UserModelAdmin(admin.ModelAdmin):
    actions = [delete_model]
    # ...

Leave a comment