F
F
Fedor2021-04-05 23:20:43
Django
Fedor, 2021-04-05 23:20:43

How to use Django ORM on remote machines?

Greetings!
- - -
There is a server with Django and a DB. There are several remote machines.
I want to run scripts on remote machines that will work with a database on a server . At the same time, using ORM objects, the same as on the server .
In other words, I need the work with database objects on the server and on remote sites to be no different.
- - -
Question:
Is it possible to use all ORM functions on remote machines?
What is the most concise way?
If you use the REST API, then I did not understand at all how to serialize objects from json back to ORM objects on the side of remote scripts.
- - -
My solution:
Haven't tested it yet, maybe I'll find a more elegant solution.
Copy the django project to remote sites , and change the ip address of the database in the project settings.
To initialize the environment at the beginning of each script, write:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")
try:
    from django.core.wsgi import get_wsgi_application
    get_wsgi_application()
except RuntimeError:
    pass

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Konovalov, 2021-04-06
@wtfedor

Well, when configuring the database in the Django application, you can specify the IP address and port of the database host, which, in general, can be anything. And that you go from your server to another server with a database, that from some working machine - the essence is the same.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question