0

I have a django project, this is the installed apps entry in settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crawler',
]

when I run python manage.py migrate everything seems to be fine. But when I try to login into django admin page, It says

(1146, "Table 'stock_db.django_session' doesn't exist")

It seems to me that migrate is not creating django admin databases, but why? I even tried deleting the database, creating it again and running python manage.py makemigrations and python manage.py migrate, It didn't create django_session table.

6
  • Did you do python manage.py makemigrations before migrate ? Commented Aug 7, 2021 at 8:23
  • @Ram Yes I did. Commented Aug 7, 2021 at 8:39
  • There is no stock_db app provided by Django, your error talks about a table stock_db.django_session not existing. And given that there is no stock_db app in your INSTALLED_APPS the error is not surprising... Commented Aug 7, 2021 at 8:51
  • @AbdulAzizBarkat stock_db is the name of the database. Commented Aug 7, 2021 at 8:58
  • Check this out stackoverflow.com/questions/10515808/… Commented Aug 7, 2021 at 9:05

3 Answers 3

1

Migrating your models won't create Admin resources. Those need to be created separately in your admin.py. See more details here https://docs.djangoproject.com/en/4.1/ref/contrib/admin/

Essentially you need to create a Resource class, specify the fields, define whatever you want:

class MyModelResource(resources.ModelResource):

class Meta:
    model = MyModel

fields = [
    "your_fields",
]

Finally, you need to register your resource:

admin.site.register(MyModelResource)

Sign up to request clarification or add additional context in comments.

Comments

0

Try makemigrations first if it doesnt help continue reading

This type of problem can appear in many situations, most common one is that your migration file system has broken. Reason may be that you tried to edit migration files, or you deleted some of them, or if you work on big teams there may be conflicting migrations. Fixing migrations system takes a lot of time and effort, plus you should know how they work. If project is not in production and you just playing around, I recommend to clear db and delete migration files and run makemigrations and migrate again. Also be aware if you have multiple apps with models referencing each other with foreign keys or whatever.

3 Comments

How can I delete the migrations of django admin app?
migration of admin app resides in python packages directory. but i would not recommend deleting migrations, just delete the migrations of the apps you created
and this error comes from db not django stock_db is your schema and django_session is a table. it says that there is no table django_session in your database schema "stock_db", look at search path you provided in your django project, and have a look at your database
0

Always make sure you run makemigrations before migrate

First run your migrations using below commands:

python manage.py makemigrations appname

python manage.py migrate

If this doesn't work then delete your current migrations using Python manage.py squashmigrations and then re-run migration commands.

4 Comments

What is appname for django admin?
have you tried - python manage.py makemigrations admin
it says no changes detected in app admin
I even tried deleting the database and creating it again, but it didn't work either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.