Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin not showing models

I'm damned if I can figure out why my models are not showing in the admin.

models.py looks like this:

from django.db import models

class Publication(models.Model):
    title = models.CharField(max_length=255)

    def __unicode__(self):
        return unicode(self.title)

admin.py

from django.contrib import admin
from publications.models import Publication

admin.site.register(Publication)

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',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)    


INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'south',
    'publications',
)

Directory structure:

xrdb/
├── manage.py
├── publications
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── xrdb
    ├── __init__.py
    ├── admin.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

And from the admin page, I can see Groups, Users, and Sites. No Publications!!

Any help?? Thanks!!

EDIT: I've already synced and migrated and admin.autodiscover() is in urls.py.

EDIT II: Updated files above. Added directory structure.

like image 934
Rob L Avatar asked Jul 17 '14 21:07

Rob L


1 Answers

Did you run syncdb after changing your models.py file?

Go to the base directory of your project (where manage.py is) and run

python manage.py syncdb

Edit: Since you already tried this, add a str or unicode method to your class like this:

class Resource(models.Model):

    title = models.CharField(max_length=128, blank=True)

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

Edit 2: Funnily enough, I ran into the exact same issue today working on my own projects. If the str code above still doesn't work for you, double check that you've actually registered your models in admin.py:

from MyApp.models import MyModel
admin.site.register(MyModel)

Edit 3: Finally, make sure your admin.py file with the above code is actually inside the directory of the app in question (i.e. the same directory with your models.py that defines MyModel).

like image 147
Gravity Grave Avatar answered Sep 20 '22 19:09

Gravity Grave