Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Serhat Teker
Serhat Teker

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]:
Enter fullscreen modeExit fullscreen mode

Solution

You should useIPython indjango shell. Just run:

$python manage.py shell-i ipython
Enter fullscreen modeExit fullscreen mode

You can also look at this post:How to Run Django Shell in Python for detail.

All done!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Uomo Universale | Software Engineer | Entrepreneur | builds systems | py:go:js |
  • Location
    127.0.0.1
  • Work
    Software Engineer
  • Joined

More fromSerhat Teker

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp