P
P
Pythonicless2014-12-05 12:54:57
Django
Pythonicless, 2014-12-05 12:54:57

What is the best way to access an external database from Django?

Hello!
I have a Python/Django project with a local sqlite3 database. During operation, the application must access and receive data from a database that has nothing to do with the project.
That is, the wonderful Django ORM cannot be used.
At the moment, I have a sql folder in the project directory, in which queries are stored in their pure form, and in the project I use a self-written module that connects the necessary driver and performs all the routine work. As a result, in the view it all comes down to

result = global_sql_driver.request('last_orders.sql')

And the question itself (more precisely, several):
How correct is this? How to do better? How to do it right? How are you doing?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
B
bromzh, 2014-12-05
@Pythonicless

That is, the wonderful Django ORM cannot be used.
Why? In models, you can describe table names, column names, etc. ORM itself will make requests. As a last resort, there are all sorts of query managers and other garbage like the Q class in the ORM, with which you can map the base table in the model.

S
Singularity, 2014-12-05
@Singularity

You can generate a models.py file with the current database.
https://docs.djangoproject.com/en/dev/howto/legacy...

D
Danila, 2017-03-28
@Machinez

why do you need an id property in an object if you are working with an array? use index.
to update user properties, you do not need to iterate over the entire array

function UpdateUser(id, name, email) {
   usersData[id].name = name;
   usersData[id].email = email;
}

Well, if you really need to work with the id property, then:
function UpdateUser(id, name, email) {
   usersData.forEach(function (item, i, usersData) {
     if (userData[i].id === id) {
       usersData[i].email = email;
       usersData[i].name = name;
     }
   });
}

I
Im p3l, 2017-03-28
@LoranDeMarcus

Found the error myself

var usersData = [];
var userObj = {name: '', email: '',id: i = 0};

function AddUser(name, email, id) {
    usersData.push({
        name: name,
        email: email,
        id: i++
    });
}

function UpdateUser(id, name , email) {
    usersData.forEach(function (item, i, usersData) {
       usersData[id].email = email;
       usersData[id].name = name;
    })
}

function DeleteUser(id) {
    delete usersData[id];
}

function ShowUsers() {
    console.log(usersData);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question