ipython에서 사용자를 얻는 방법 - Django

3937 단어 shellpythondjango

문제



Django 앱에서 User Model를 얻으려고 하지만 대신 ImproperlyConfigured 오류가 발생합니다.

오류의 상세 모드는 다음과 같이 말합니다.

ImproperlyConfigured: Requested setting AUTH_USER_MODEL, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.



워크플로는 다음과 같습니다.

(.venv) ~/yadrta $ ipython
Python 3.8.1 (default, Feb 12 2020, 16:30:11)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.18.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from django.contrib.auth import get_user_model

In [2]: User = get_user_model()
---------------------------------------------------------------------------
ImproperlyConfigured                      Traceback (most recent call last)
<ipython-input-2-6968e3c2733c> in <module>
----> 1 User = get_user_model()

~/yadrta/.venv/lib/python3.8/site-packages/django/contrib/auth/__init__.py in get_user_model()
    155     """
    156     try:
--> 157         return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
    158     except ValueError:
    159         raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'")

~/yadrta/.venv/lib/python3.8/site-packages/django/conf/__init__.py in __getattr__(self, name)
     81         """Return the value of a setting and cache it in self.__dict__."""
     82         if self._wrapped is empty:
---> 83             self._setup(name)
     84         val = getattr(self._wrapped, name)
     85         self.__dict__[name] = val

~/yadrta/.venv/lib/python3.8/site-packages/django/conf/__init__.py in _setup(self, name)
     62         if not settings_module:
     63             desc = ("setting %s" % name) if name else "settings"
---> 64             raise ImproperlyConfigured(
     65                 "Requested %s, but settings are not configured. "
     66                 "You must either define the environment variable %s "

ImproperlyConfigured: Requested setting AUTH_USER_MODEL, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

In [3]:


해결책



IPython 에서 django shell 을 사용해야 합니다. 그냥 실행:

$ python manage.py shell -i ipython


자세한 내용은 이 게시물How to Run Django Shell in Python을 참조하세요.

모두 완료되었습니다!

좋은 웹페이지 즐겨찾기