If I have a PostgresDB that contains both Django models and other SQL tables, is it possible to register these other SQL tables in the Django admin panel?
More details about the setup: I have a docker-compose setup where Django is running in one container, a Postgres DB in another, and a slack-app in a third container. Django is connected to the DB and the models are registered in the admin panel. This works as intended. The slack-app is also connected to the same DB and has some tables there that are not Django-models. I would like to also access these through the Django admin panel in order to have everything in one place. Is this possible?
CodePudding user response:
You can define unmanaged models in Django. These models will not construct migrations, but will only query the database to select, insert, etc.
Django offers a tool inspectdb
[Django-doc] to inspect the database and write the corresponding unamanged models. You thus can use this with:
python3 manage.py inspectdb table1 table2 tablen
It will then write the corresponding models for these tables to the standard output channel, and you thus can copy these in the models.py
. In the Meta
of these models it will add a managed = False
to denote that Django will not migrate these models.
Once you registered these models, you can register a ModelAdmin
with:
from django.contrib import admin
from app_name.models import Model1, Model2, Modeln
admin.site.register(Model1)
admin.site.register(Model2)
admin.site.register(Modeln)