Django管理员不显示模型 [英] Django admin not showing models

查看:161
本文介绍了Django管理员不显示模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



models.py看起来像这样:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $



$ b $ max_length = 255)

def __unicode __(self):
return unicode(self.title)

admin.py

 从django.contrib导入admin 
from publications.models import出版物

admin.site.register(出版)

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',
#Unc下一行的简单点击保护:
#'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',
#取消注释下一行以启用管理员:
'django.contrib .admin',
#取消注释下一行以启用管理员文档:
#'django.contrib.admindocs',
'south',
'publications',

目录结构:

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

从管理页面,我可以请参阅组,用户和站点。没有出版物!!



任何帮助?
谢谢!!



编辑:我已经同步并迁移,admin.autodiscover()在urls.py中。



编辑II:上面更新的文件。添加目录结构

解决方案

更改models.py文件后是否运行syncdb?



转到项目的基础目录(manage.py是),然后运行

  python manage。 py syncdb 

编辑:既然你已经尝试过了,请像这样添加一个str或unicode方法到你的类:

$ b

  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
pre>

编辑2:有趣的是,我遇到了同样的问题,在我自己的项目上工作。如果上面的 str 代码仍然不适合您,请仔细检查您是否在admin.py中实际注册了模型:

 从MyApp.models导入MyModel 
admin.site.register(MyModel)

编辑3:最后,确保您的具有上述代码的admin.py文件实际上位于相关应用程序的目录(即与您的models.py的MyModel相同的目录中)。


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.

解决方案

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

这篇关于Django管理员不显示模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆