Arun
Arun

Reputation: 2003

Django: Cannot create User inside ipython shell

I am trying to import Django User model in ipython inside virtual environment. I have tried the following code

from django.contrib.auth.models import User

It resulted in the following error

AppRegistryNotReady: Apps aren't loaded yet.

Then from this answer, I have setup inside the shell and tried the following code.

from django.conf import settings
User = settings.AUTH_USER_MODEL
User.objects.create_user('john', '[email protected]', 'johnpassword')

This resulted in AttributeError: 'str' object has no attribute 'objects' . How can I create a user inside ipython shell? I am using Django 1.10.6, ipython 6.0.0 and djangorestframework 3.6.2

Upvotes: 1

Views: 312

Answers (2)

zaidfazil
zaidfazil

Reputation: 9235

settings.AUTH_USER_MODEL is a string, inside shell you can import User as model from django.

For this, run python manage.py shell

Or you could manually import django and set the DJANGO_SETTINGS_MODULE.

For that, inside ipython run these,

import django, os
os.environ['DJANGO_SETTINGS_MODULE'] = 'testproject.settings'
django.setup()

Then you can do,

from django.contrib.auth.models import User

then you can create users inside shell like this,

User.objects.create_user('john', '[email protected]', 'johnpassword')

Upvotes: 2

Arun
Arun

Reputation: 2003

I have solved the problem by starting the ipython shell using the command

python manage.py shell -i ipython

Upvotes: 1

Related Questions