1

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', 'lennon@thebeatles.com', '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

Arun
  • 1,933
  • 2
  • 28
  • 46
  • Try only this: User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword') -> this works for me. I would suggest you to user "shell_plus" – FrankTheTank_12345 May 30 '17 at 09:41
  • Well, AUTH_USER_MODEL is a string. Why are you doing this? If you start your shell via `manage.py shell` you can import User as normal. – Daniel Roseman May 30 '17 at 09:42

2 Answers2

2

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', 'lennon@thebeatles.com', 'johnpassword')
zaidfazil
  • 9,017
  • 2
  • 24
  • 47
1

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

python manage.py shell -i ipython
Arun
  • 1,933
  • 2
  • 28
  • 46