问题
I would like to allow people that don't have access to the Django project root folder (so no access to settings.py
file and no DB password) to use specific models outside of Django.
I would like some users to be able to query certain tables in the database using the powerful Django structure (querysets etc...) without giving full access to all the tables.
Would any of the following strategies work? Is it even possible to do that? And what are the best practices for this kind of issues?
Idea 1: Setting-up django using my_project.settings
.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
django.setup()
The problem is that it requires access to the project root folder and give access to all the database
Idea 2: Setting-up django using a different database user with restricted access
from django.conf import settings
settings.configure(
DATABASE_ENGINE = 'django.db.backends.mysql',
DATABASE_NAME = '<db_name>',
DATABASE_USER = 'new_user',
DATABASE_PASSWORD = '<psw>',
DATABASE_HOST = '<host>',
INSTALLED_APPS = ...
)
I didn't manage to make this work yet but I feel like the django.setup()
will fail because the DB user won't have access to all the tables. Also it still needs access to the project root folder.
Idea 3: Using the database (MySQL) directly from python, no link at all with Django.
This is probably not advised but that's the only solution I have so far. Is there any django-like python package that would allow querying the database in a similar way to Django?
Idea 4: Maybe something using a custom manage.py
command?
来源:https://stackoverflow.com/questions/51158299/django-models-access-outside-django-without-access-to-the-settings-file