M
M
msnyastar2020-04-23 00:24:07
Django
msnyastar, 2020-04-23 00:24:07

Accessing the same database from different Django projects?

Hello! There are two projects:
Project-1 - PostgreSQL
Project-2 - SQLite
I want to make a request from Project-2 in PostgreSQL related to the first project.
To do this, Project-2 settings added additional database settings to the settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
                },
    'front': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'site_db_v2',
        'USER': 'alexandr',
        'PASSWORD': '*********',
        'HOST': '10.20.1.156',
        'PORT': '5432',
                }
            }

In Project-2, I added a model, as in the first project, only redefined the names:
class Meta:
        app_label = 'exlorer'
        db_table = 'exlorer_btcaddresse'


On Project-2, I did not carry out any migrations in relation to the additional base ('front' - the main one for Project-1).
As a result, when I make a request, I get an error:
BtcAddresses.objects.using('front').all()
Internal Server Error: /
Traceback (most recent call last):
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "exlorer_btcaddresse" does not exist
LINE 1: ...resse"."id", "exlorer_btcaddresse"."address" FROM "exlorer_b...
                                                             ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\PyProjects\Django_back\notification\monitor_events\views.py", line 11, in index
    print(BtcAddresses.objects.using('front').all())
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\db\models\query.py", line 252, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\db\models\query.py", line 276, in __iter__
    self._fetch_all()
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\db\models\query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\db\models\query.py", line 57, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1151, in execute_sql
    cursor.execute(sql, params)
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\PyProjects\Django_back\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "exlorer_btcaddresse" does not exist
LINE 1: ...resse"."id", "exlorer_btcaddresse"."address" FROM "exlorer_b...
                                                             ^

[23/Apr/2020 00:19:32] "GET / HTTP/1.1" 500 129001


Can you please tell me how to make a query to the database from Project-1?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Tikhonov, 2020-04-23
@msnyastar

Well, in general, you have already succeeded, but most likely there is a typo in the table name. The error is directly related to postgres.

M
msnyastar, 2020-04-23
@msnyastar

Yes, it seems. I get a normal response to the request without exception ... I will figure it out ...
User.objects.using('front').all()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question