Django models access outside Django without access to the settings file

拜拜、爱过 提交于 2020-01-06 07:27:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!