74
votes

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: Registered models do not show up in admin 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)),
)
27
Stop upvoting this post already. I now am deeply ashamed of it :DOdif Yltsaeb

27 Answers

27
votes

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'
162
votes

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)
13
votes

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.

10
votes

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 :)

6
votes

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)
4
votes

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.

4
votes

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)
3
votes

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

2
votes

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

2
votes

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

1
votes

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.

1
votes

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

1
votes

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

1
votes

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.

1
votes

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
0
votes

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',
)
0
votes

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

0
votes

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

0
votes

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.

0
votes

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.

0
votes

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.

0
votes

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.

0
votes

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.

0
votes

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.

0
votes

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

0
votes

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

-1
votes

You only have to import(include) this:

from models import *