model not showing up in django admin

DjangoDjango Admin

Django Problem Overview


I have ceated several django apps and stuffs for my own fund and so far everything has been working fine.

Now I just created new project (django 1.2.1) and have run into trouble from 1st moments.

I created new app - game and new model Game. I created admin.py and put related stuff into it. Ran syncdb and went to check into admin. Model did not show up there.

I proceeded to check and doublecheck and read through previous similar threads: https://stackoverflow.com/questions/1839927/registered-models-do-not-show-up-in-admin https://stackoverflow.com/questions/1694259/django-app-not-showing-up-in-admin-interface

But as far as I can tell, they don't help me either. Perhaps someone else can point this out for me.

models.py in game app:

# -*- coding: utf-8 -*-
from django.db import models

class Game(models.Model):
      type = models.IntegerField(blank=False, null=False, default=1)
      teamone = models.CharField(max_length=100, blank=False, null=False)
      teamtwo = models.CharField(max_length=100, blank=False, null=False)
      gametime = models.DateTimeField(blank=False, null=False)

admin.py in game app:

# -*- coding: utf-8 -*-
from jalka.game.models import Game
from django.contrib import admin

class GameAdmin(admin.ModelAdmin):
      list_display    = ['type', 'teamone', 'teamtwo', 'gametime']
      
admin.site.register(Game, GameAdmin)

project settings.py:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'jalka.urls'

TEMPLATE_DIRS = (
      "/home/projects/jalka/templates/"
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'game',
)

urls.py:

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
      # Example:
      # (r'^jalka/', include('jalka.foo.urls')),
      (r'^admin/', include(admin.site.urls)),
)

Django Solutions


Solution 1 - Django

The problem reported can be because you skipped registering the models for the admin site. This can be done, creating an admin.py file under your app, and there registering the models with:

from django.contrib import admin
from .models import MyModel

admin.site.register(MyModel)

Solution 2 - Django

Hmmmm...Try change include of your app in settings.py:

From:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'game',
    ....

To:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'YOUR_PROJECT.game',# OR 'YOUR_PROJECT.Game'

Solution 3 - Django

Had the same issue with Django 2.0.

The following code didn't work:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from users import models

admin.register(models.User, UserAdmin)

It turns out the line

admin.register(models.User, UserAdmin)

should have been

admin.site.register(models.User, UserAdmin)

Since I didn't get any warnings, just wanted to point out this thing here too.

Solution 4 - Django

I know this has already been answered and accepted, but I felt like sharing what my solution to this problem was, maybe it will help someone else.

My INSTALLED_APPS looked like this:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',  # <---- this is my custom app
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    'south',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

See, I put my app before Django's admin app, and apparently it loads them in that order. I simply moved my app right below the admin, and it started showing up :)

Solution 5 - Django

For Django 1.10 helped to me to register the Model following way with (admin.ModelAdmin) at the end

from django.contrib import admin

from .models import YourModel
admin.register(YourModel)(admin.ModelAdmin)

Solution 6 - Django

It's probably very rare but I had an issue today where the permissions on the admin.py file I had created were corrupt and thus made it unreadable by django. I deleted the file and recreated it with success.

Hope that saves someone, should they stumble here with my problem.

Solution 7 - Django

I struggled with registering my models (tried all the suggestions above) too until I did a very simple thing. I moved my admin.py out of the directory up to the project directory - refreshed the admin screen and moved it back and voila into the models app directory - they all appeared instantly. I'm using PyCharm so not sure if that was causing some problems.

My setup is exactly what the Django manual says -

models.py

class xxx(models.Model):
    ....
    def __str__(self):  # __str__ for Python 3, __unicode__ for Python 2
        return self.name

admin.py

from .models import xxx
admin.site.register(xxx)

Solution 8 - Django

I am using digital ocean and also ran into this issue. The solution was a service restart. I used

service gunicorn restart

and that got the model to show up

Solution 9 - Django

I have the same problem. I solve this to add register of admin to admin.py. And I don't need to add extra class.

Like:

from jalka.game.models import Game
from django.contrib import admin

admin.site.register(Game)

Enviroment: Win10、Django 1.8

[tag:django]

Solution 10 - Django

I had the same issue and i resolved it as follow first go to your app.py file and copy the class name.

from django.apps import AppConfig

class **ResumeConfig**(AppConfig):
name = 'resume'

now go to setting.py and place it in application definition as follow:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
**'resume.apps.ResumeConfig'**
]

save and run the commands in the terminal

python manage.py makemigrations
python manage.py migrate

run your server and go to /admin your problem will be solved

Solution 11 - Django

you just need to register your model. got to admin.py file of the same app and write,

from .models import "Modelname"

admin.site.register(("Modelname")) 
#create tuple to register more than one model

Solution 12 - Django

I had the same issue. Few of my models were not showing in the django admin dashbaord because I had not registered those models in my admin.py file.

//Import all the models if you have many

from store.models import *

admin.site.register(Order)
admin.site.register(OrderItem)

This fixed it for me. Just register and refresh your webpage and the models will be visible in the admin dashboard

Solution 13 - Django

I would also like to add, that I did everything these answers said, except I did not enter on the command line sudo service apache2 restart which I needed to make the changes take effect. (Because I was testing on a live server on port 80 on amazon web services. I was also using an Ubuntu operating system.) That solved it for me. I hope this might help somebody.

Solution 14 - Django

I got the same issue, After restart my development server model is showing in admin page.

Solution 15 - Django

I just had to restart apache service. service apache2 restart. Then new models shown up.

Solution 16 - Django

Change your admin code to this:

from django.contrib import admin
from .models import Game

class GameAdmin(admin.ModelAdmin):
  list_display    = ['type', 'teamone', 'teamtwo', 'gametime']

admin.site.register(Game, GameAdmin)

Also make sure the app is included in settings.py file and make sure you're importing the right name for your model. Another reason that can bring about this issue is putting a comma (,) at the end of your model fields as though it a python list.

Solution 17 - Django

Adding to what Saff said, your settings.py should be like this:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'YOUR_PROJECT',
    # And
    'YOUR_PROJECT.Foo',
    'YOUR_PROJECT.Bar',
)

Solution 18 - Django

The mistake might be at views.pyin your "def" you should check if you got

If mymodel.is_valid():
mymodel = model.save()

I will give you a piee of my code so you would understand it

@login_required(login_url='/home/')
def ask_question(request):
user_asking = UserAsking()

if request.method == "POST":
    user_asking = UserAsking(request.POST)

    if user_asking.is_valid():
        user_asking.save()

    return redirect(home)

return render(request, 'app/ask_question.html', {
    'user_asking': user_asking,
})

`UserAsking is a form. i hope i did help you

Solution 19 - Django

One of the possible reasons is that you do not close old python processes.

Solution 20 - Django

I had a similar issue on an instance of django mounted in a docker container. The code was correct but changes to admin.py were not appearing in django admin.

After quitting the django process and remounting the container with docker-compose up, the added models became available in django admin.

Solution 21 - Django

My new model not showing up on Admin page due to me using ADMIN_REORDER(https://pypi.org/project/django-modeladmin-reorder/) in settings.py to control model order, added a while back and forgotten. Added the new model to settings.py and it shows up.

Solution 22 - Django

I had all my Admin classes in a folder ./sites/ which only worked because of cercular dependencies I had. I fixed them, and everything else stopped working.

When I renamed that directory from ./sites/ to ./admin/ it worked.

Solution 23 - Django

Another thing to point out is that you should make sure you're signed in with a superuser.

It could be that you are signed in with a user who has limited permission on view capacity so try that.

I in Django 2.0 I still register my app using just the name of the app i.e 'game'. But the issue was in the logged in user permissions.

Solution 24 - Django

I was updating a remote server with code from local, but the remote server was still using the old models in the admin screen. Everything looked exactly the same between my local and the remote server - the database tables matched and the code was also identical.

The problem turned out the remote server was running using the venv environment. After running source venv/bin/activate and then doing the migrations, the admin showed all the new models.

Solution 25 - Django

If you have multiple models to register then use.

admin.site.register((Model1 , Model2 , Model3))

or

admin.site.register([Model1 , Model2 , Model3])

in your admin.py file.

Solution 26 - Django

I had the same issue. Done everything right. But I didn't have permission to view the model.

Just give the required permission (view/add/edit/etc) and then check again.

Give Permission using admin panel:

  1. Login to the site using the credentials for your superuser account.
  2. The top level of the Admin site displays all of your models, sorted by "Django application". From the Authentication and Authorization section, you can click the Users or Groups links to see their existing records.
  3. Select and add the required permission

Select and add the required permission enter image description here

refer this to add/change permission through the admin panel

Alternative: Using code refer Official Django Doc

Solution 27 - Django

I have the same error but I have not mentioned my app in INSALLED_APPS

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'myapp.apps.myappConfig',
)

I have already registered the model but forgot to mention my app in INSTALLED_APPS and after mentioning it works for me

Solution 28 - Django

For me, migration was an issue. If you created this new app, try doing the following:

python manage.py makemigrations <name_of_your_app>
python manage.py migrate <name_of_your_app>

You do not need to include all admin.site.register in a single app, you could specify them in their corresponding app at the admin.py file.

Solution 29 - Django

I had this same issue, django=4.0 with docker-compose i was not using ModelAdmin, turns out registering multiple Models in one call to admin.site.register errors out when running migration.

So doing

admin.site.register(ModelA, ModelB)

did not work. instead, you I did something like

admin.site.register(ModelA)
admin.site.register(ModelB)

I hope it helps anyone dockerizing their Django App

Solution 30 - Django

Give your user "Superuser status" through the edit user functionality in the admin panel.

enter image description here

Most probably the user you are signed in is not a superuser and doesn't have the required permissions for the newly created models.

In Django, after new models are created and migrated, the permissions of the existing users are not updated. Only the users with Superuser status can by default view, add, change and delete records for these models.

To fix the problem and avoid it in the future give "Superuser status" to your development user.

Solution 31 - Django

You only have to import(include) this:

from models import *

Solution 32 - Django

You Make sure You have Added your Model to admin.py file .

Example : if your model is Contact.

from django.contrib import admin

from .models import Contact
admin.site.register((Contact))

Now your issue might be resolved if not try restarting your server.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOdif YltsaebView Question on Stackoverflow
Solution 1 - Djangojesicadev18View Answer on Stackoverflow
Solution 2 - DjangoSaffView Answer on Stackoverflow
Solution 3 - DjangoPavel VergeevView Answer on Stackoverflow
Solution 4 - DjangoEduard LucaView Answer on Stackoverflow
Solution 5 - DjangopymenView Answer on Stackoverflow
Solution 6 - DjangobitwitView Answer on Stackoverflow
Solution 7 - DjangoRob MascaroView Answer on Stackoverflow
Solution 8 - DjangoMichael FoxView Answer on Stackoverflow
Solution 9 - Djangosos418View Answer on Stackoverflow
Solution 10 - DjangoMUHAMMAD MESUMView Answer on Stackoverflow
Solution 11 - DjangoANURAG SHARMAView Answer on Stackoverflow
Solution 12 - DjangoOm Prakash NView Answer on Stackoverflow
Solution 13 - DjangoRock LeeView Answer on Stackoverflow
Solution 14 - DjangoMuthuraj SView Answer on Stackoverflow
Solution 15 - Djangoselim atmacaView Answer on Stackoverflow
Solution 16 - DjangoRomeoView Answer on Stackoverflow
Solution 17 - DjangophreeLynxView Answer on Stackoverflow
Solution 18 - Djangodanil2000View Answer on Stackoverflow
Solution 19 - DjangoДмитрий ВинникView Answer on Stackoverflow
Solution 20 - DjangoFlyingJayView Answer on Stackoverflow
Solution 21 - DjangoEmilyView Answer on Stackoverflow
Solution 22 - DjangoDaniel W.View Answer on Stackoverflow
Solution 23 - Djangoinyang kpongetteView Answer on Stackoverflow
Solution 24 - DjangoOliverView Answer on Stackoverflow
Solution 25 - DjangoJaspreet singhView Answer on Stackoverflow
Solution 26 - DjangoKevalView Answer on Stackoverflow
Solution 27 - DjangoMuhammad NomanView Answer on Stackoverflow
Solution 28 - DjangoHossein KalbasiView Answer on Stackoverflow
Solution 29 - DjangoThe AmbidextrousView Answer on Stackoverflow
Solution 30 - DjangosvetView Answer on Stackoverflow
Solution 31 - Djangouser10149671View Answer on Stackoverflow
Solution 32 - DjangoUtkarsh vermaView Answer on Stackoverflow