0

After running a successful runserver,
To create a super user in django project, I updated the admin.py in my app directory as

from django.contrib import admin

from .models import Item

admin.site.register(Item)

by running the following

$: python manage.py createsuperuser; I am getting the below error

errorlog pastebin

I am newbie to django and python, I read in other post about circular import but couldn't figure out error.

I have taken the tutorial from Python and django youtube.

krishnakant
  • 355
  • 1
  • 4
  • 17

3 Answers3

1

Do Not Update manage.py

There must be a file called admin.py in your app folder. Whenever you create a new app, admin.py is automatically cerated in your app directory (django-admin startapp your_app_name )

open admin.py: move your code into it.

from django.contrib import admin

from .models import Item  # Make sure you update this properly.

admin.site.register(Item)

Now Item should be listed in your admin panel.

PS: Make sure there are no circular import in your models. The error you are getting is most likely resulting from a circular import.

Justin M. Ucar
  • 853
  • 9
  • 17
  • Sorry a typo, I am updating the admin.py. Updated the question with the same. – krishnakant Jul 25 '16 at 19:06
  • @krishnakant well in this case, it would be best to see what is in your models.py file – Justin M. Ucar Jul 25 '16 at 19:08
  • 'from django.db import models class Item(models.Model): title = models.CharField(max_length=200) description = models.TextField() amount = models.IntegerField() ' models.py – krishnakant Jul 25 '16 at 19:14
  • @krishnakant are you importing your Item model in your app folders __ __init__ __.py ? – Justin M. Ucar Jul 25 '16 at 19:14
  • file __init__.py is empty. I am not importing anything – krishnakant Jul 25 '16 at 19:17
  • @krishnakant try turning your models.py into a module: create a folder called models in the same dir where models.py is and create a file called item.py and place your Item Model class in it. Dont forget to remove models.py. Last create ____init____.py in models folder and do `from .item import Item` then in admin.py do `from models import Item` – Justin M. Ucar Jul 25 '16 at 19:22
  • do i have to remove init, admin, models original files ? – krishnakant Jul 25 '16 at 19:28
  • @krishnakant you only remove the models.py to avoid ambugiation between models.py and models folder(module). tou don't remove ____init____.py or any other. – Justin M. Ucar Jul 25 '16 at 19:29
  • @krishnakant like this one here: http://stackoverflow.com/questions/6336664/split-models-py-into-several-files – Justin M. Ucar Jul 25 '16 at 19:40
  • Thanks for helping. I have not defined a model Item in models.py as mentioned by IanAuld. – krishnakant Jul 25 '16 at 20:08
1

You have identified the problem in this comment:

models.py : from django.db import models

Item is not something that comes with Django. You will need to define a model named Item yourself. My guess is you ahve missed a pretty important step in your tutorial (or the tutorial is wrong/missing a step). However to get your app running in the meantime add to models.py:

from django.db import models


class Item(models.Model):
    pass

This should allow you to create a superuser. Be aware this model doesn't do anything. You will either have to find the missing step of your tutorial or figure out what it supposed to be doing.

kylieCatt
  • 10,672
  • 5
  • 43
  • 51
0

Is your manage.py and models.py in the same directory? from .models import Item means you are trying to import the models from the same package(relative import), if not then you might need to do this(substitute app with your actual app name):

from app.models import Item

Also, it's really bad to register admin in manage.py. Django suggest doing it in the admin.py file in your app. Since admin.py is in the same package as models.py, it should just work. Check django doc about this.

Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • I just checked the link in your video, it doesn't even say that you should do it in `manage.py`, it clearly says it's in the file `admin.py`. – Shang Wang Jul 25 '16 at 19:01
  • i am updating the admin.py, Updated the question with the correct file name. Thanks for pointing this out. – krishnakant Jul 25 '16 at 19:03
  • Did you have a model called `Item` in `models.py`? Did you have `__init__.py` file in your app? Did you restart your server when you change your code? – Shang Wang Jul 25 '16 at 19:06
  • yes i have 'class Item(models.Model):' in models.py along with __init__.py file in my app. I restarted the server. – krishnakant Jul 25 '16 at 19:12