django Mongodb connector 설정하기

Django mongodb connector 설정하기

참고문서: https://nesdis.github.io/djongo/get-started/

사전작업: mongodb 설치, django3.1 설치.

1. django-mongodb connector(djongo) 설치.
pip 로 django를 설치하면 아래처럼 몇몇 패키지들은 제거되고 django3.0.5가 설치된다. django 3.1버전이 지원되지 않아서 그런듯 하다.

pip install django
Collecting django
  Using cached https://files.pythonhosted.org/packages/2b/5a/4bd5624546912082a1bd2709d0edc0685f5c7827a278d806a20cf6adea28/Django-3.1-py3-none-any.whl
Collecting pytz (from django)
  Using cached https://files.pythonhosted.org/packages/4f/a4/879454d49688e2fad93e59d7d4efda580b783c745fd2ec2a3adf87b0808d/pytz-2020.1-py2.py3-none-any.whl
Collecting sqlparse>=0.2.2 (from django)
  Using cached https://files.pythonhosted.org/packages/85/ee/6e821932f413a5c4b76be9c5936e313e4fc626b33f16e027866e1d60f588/sqlparse-0.3.1-py2.py3-none-any.whl
Collecting asgiref~=3.2.10 (from django)
  Using cached https://files.pythonhosted.org/packages/d5/eb/64725b25f991010307fd18a9e0c1f0e6dff2f03622fc4bcbcdb2244f60d6/asgiref-3.2.10-py3-none-any.whl
Installing collected packages: pytz, sqlparse, asgiref, django
Successfully installed asgiref-3.2.10 django-3.1 pytz-2020.1 sqlparse-0.3.1
...

Requirement already satisfied: asgiref~=3.2 in d:\python_project\bctools\venv\lib\site-packages (from django<=3.0.5,>=2.1->djongo) (3.2.10)
Requirement already satisfied: pytz in d:\python_project\bctools\venv\lib\site-packages (from django<=3.0.5,>=2.1->djongo) (2020.1)
Installing collected packages: sqlparse, pymongo, django, djongo
  Found existing installation: sqlparse 0.3.1
    Uninstalling sqlparse-0.3.1:
      Successfully uninstalled sqlparse-0.3.1
  Found existing installation: Django 3.1
    Uninstalling Django-3.1:
      Successfully uninstalled Django-3.1
  Running setup.py install for djongo ... done
Successfully installed django-3.0.5 djongo-1.3.3 pymongo-3.11.0 sqlparse-0.2.4

2. django 프로젝트 디렉토리의 settings.py 에서 mongodb를 사용하도록 설정한다.
기본 데이타베이스인 sqlite3 부분은 주석처리하고, djongo를 설정한다. 데이타베이스 이름은 bctools로 했다.

DATABASES = {
    # 'default': {
    #     'ENGINE': 'django.db.backends.sqlite3',
    #     'NAME': BASE_DIR / 'db.sqlite3',
    # }
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'bctools',
    }
}

3. database 생성과 superuser 만들기

manage.py makemigrations
manage.py migrate

manage.py createsuperuser
Username (leave blank to use 'works'):
Email address:
Password:
Password (again):

이제 manage.py runserver 명령을 실행하고 웹브라우저에서 localhost:8000/admin 으로 접속하면 관리자 페이지를 볼 수 있다.

답글 남기기

Your email address will not be published.