django set client encoding with multiple database

1

Good I am developing a API by django rest framework and for this I have configured two databases. I need to make a query to the second database configured (postgresql) and get it to return the data but it gives me the special characters without decoding. For example for the word "mathematics" it returns "Matem\u00e1ticas" .

I have tried to make a query outside the environment of django and using the method conn.set_client_encoding('UTF8') the problem is solved but in django I can not find any similar method. I attach the code that works with me outside of django :

import psycopg2

try:
    conn = psycopg2.connect("dbname='XXX' user='XXX' host='XXX' password='XXX'")
except:
    print("No ha sido posible conectar a la base de datos")

conn.set_client_encoding('UTF8')
cur = conn.cursor()
cur.execute("""XXX""")
rows = cur.fetchall()
...

And that's how I establish connection to the database in django

conn = connections['XXX']
cur = conn.cursor()
cur.execute("""XXX""")
rows = cur.fetchall()
...

Would there be any equivalent in django to conn.set_client_encoding('UTF8') ?

I hope someone can help me and thank you.

    
asked by Alberto 21.12.2017 в 17:46
source

1 answer

0

To solve this problem, edit the python configuration file, but as you say that you can not access the database, you will have to ask the administrator of the database to do it.

IN PYTHON 2.6 /usr/lib/python2.6/site.py on line 456 where it says:

456: encoding="ascii" # Default value set by _PyUnicode_Init ()

We change by: 456: encoding="utf-8" # Default value set by _PyUnicode_Init ()

IN PYTHON 2.7 /usr/lib/python2.7/site.py on line 493 where it says:

493: encoding="ascii" # Default value set by _PyUnicode_Init ()

We change by: 493: encoding="utf-8" # Default value set by _PyUnicode_Init ()

    
answered by 22.12.2017 в 21:32