How to fetch warnings from a django mysql query?

I'm trying to get a list of warnings after a mySQL query, using Django admin. I can see from the documentation here that it's possible to record warnings by setting connection.get_warnings to true. But I can't find anything explaining how to read those warnings.

I do not want to throw an exception - I am deleting items from the database using a DELETE IGNORE statement and want to get all instances of deletions that failed (due to external keys, etc.)

I've tried returning the result of the execute function itself (just gave me a number) and calling fetchwarnings() on the cursor (threw a "Cursor object has no attribute fetchwarnings" error).

I'm still new to both Python and Django. I'm looking through all the documentation I can find but can't find anything that works.

from django.db import connection
query = "{query here}"
connection.get_warnings = True
with connection.cursor() as cursor:
    cursor.execute(query) <-- Returns a number
    return cursor.fetchwarnings() <-- Throws an error
Back to Top