'How to generate a dictionary cursor with a non-default Sqlite database in Django

In the simple Sqlite3 code below, using a non-default SQLLite database ("sqlite3_db" set up in DATABASES in settings.py) I am trying to build a dictionary cursor. I understand this is done using row_factory which, from my research requires a connection object. However, when using a non-default database, I can't figure out how this is done as the creation of a cursor from connections doesn't seem to create a connection object.

def index_sqlite(request):
    
    from django.db import connections
    import sqlite3

    cursor = connections["sqlite3_db"].cursor()

    #connection.row_factory = sqlite3.Row   ==> how to access the connection object??

    sql = "SELECT title, rating FROM book_outlet_book ORDER BY title"
    cursor.execute(sql)

    book_list = [item for item in cursor.fetchall()]

    return render(request, "book_outlet/index.html", {
        "title": "Some title",
        "books": book_list
    })

This code produces a list of tuples as expected, but I am after a "dictionary cursor" so I can do things like reference book_list[3].title (without using a model).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source