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)
3
  • isn't that should be django.db.backends.postgresql instead of django.db.backends.postgresql_psycopg2 ? Commented Oct 1, 2020 at 4: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? Commented Oct 1, 2020 at 4:51
  • Diff between postgresql_psycopg2 and postgresql ? Commented Oct 1, 2020 at 4:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.