Posted on • Originally published attech.serhatteker.com on
How to get User in ipython - Django
Problem
You are trying to getUser Model
in yourDjango app but gettingImproperlyConfigured
error instead.
Verbose mode of the error says:
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.
Your workflow would be like below:
(.venv) ~/yadrta$ipythonPython 3.8.1(default, Feb 12 2020, 16:30:11)Type'copyright','credits' or'license'formore informationIPython 7.18.1-- An enhanced Interactive Python. Type'?'forhelp.In[1]: from django.contrib.auth import get_user_modelIn[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__.pyinget_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 itinself.__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]:
Solution
You should useIPython indjango shell. Just run:
$python manage.py shell-i ipython
You can also look at this post:How to Run Django Shell in Python for detail.
All done!
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse