0

When using psycopg2 as the database connector specified in a DATABASES constant in settings.py, how do raw SQL queries get handled? When you use django.db.connection and cursor.execute(), does it use Django's classes to handle things, or psycopg2s?

Here's an example constant:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'webdev',
        'USER': 'someone',
        'PASSWORD': 'soopersekrit',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

and example raw query setup:

    from django.db import connection
    with connection.cursor() as cursor:
        sql_statement = f'''
        UPDATE cart c SET 
            apples = {apples_count},
        WHERE c.id = {table_row_pk}
        '''
        cursor.execute(sql=sql_statement)
nerdenator
  • 1,265
  • 2
  • 18
  • 35
  • isn't that should be `django.db.backends.postgresql` instead of `django.db.backends.postgresql_psycopg2` ? – JPG Oct 01 '20 at 04:48
  • @ArakkalAbu that's sort of the heart of my question. I know that Django has a default postgresql backend, but I'm using `postgresql_psycopg2`. Does the raw query get run through postgresql_psycopg2? – nerdenator Oct 01 '20 at 04:51
  • [Diff between `postgresql_psycopg2` and `postgresql`](https://stackoverflow.com/questions/47946856/what-is-the-difference-between-postgres-and-postgresql-psycopg2-as-a-database-en) ? – JPG Oct 01 '20 at 04:53

0 Answers0