model not showing up in django admin
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()
urlpatt开发者_开发技巧erns = patterns('',
# Example:
# (r'^jalka/', include('jalka.foo.urls')),
(r'^admin/', include(admin.site.urls)),
)
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)
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'
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.
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 :)
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)
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.
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
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)
I just had to restart apache service. service apache2 restart. Then new models shown up.
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
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
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
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
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.
I got the same issue, After restart my development server model is showing in admin page.
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.
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.
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:
- Login to the site using the credentials for your superuser account.
- 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.
- Select and add the required permission
Select and add the required permission
refer this to add/change permission through the admin panel
Alternative: Using code refer Official Django Doc
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',
)
The mistake might be at views.py
in 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
One of the possible reasons is that you do not close old python processes.
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.
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.
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.
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.
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.
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
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.
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
Give your user "Superuser status" through the edit user functionality in the admin panel.
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.
精彩评论