一、默认注册显示方式(显示默认的所有列):
models.py
class Article(models.Model): """文章""" title = models.CharField(max_length=50,verbose_name= '文章标题') desc = models.CharField(max_length=50,verbose_name='文章描述') content = models.TextField(verbose_name='文章内容') click_count = models.IntegerField(default=0,verbose_name='点击次数') is_recomment = models.BooleanField(default=False,verbose_name='是否推荐') date_publish = models.DateTimeField(auto_now_add=True,verbose_name='发布时间') user = models.ForeignKey(User,verbose_name='用户') category = models.ForeignKey(Category,verbose_name='分类') tag = models.ManyToManyField(Tag,verbose_name='标签') class Meta: verbose_name = '文章' verbose_name_plural =verbose_name ordering = ['-date_publish'] def __unicode__(self): return self.title
admin.py
from django.contrib import adminfrom poll.models import *# Register your models here.admin.site.register(Article,ArticleAdmin)
二、自定义显示方式:
1、fields:需要显示的列
class ArticleAdmin(admin.ModelAdmin): fields = ("title","desc","content")
2、exclude:不需要显示的列,遇上与上面一样
3、fieldsets:分组显示,分成2个块来显示。直接复制修改列明即可用。
4、list_display:显示列的信息
class ArticleAdmin(admin.ModelAdmin): list_display = ("desc","click_count") fieldsets = ( (None,{'fields':("title","desc","content",)}), ("高级设置",{"classes":("collapse",),"fields":("click_count","user")}) )
参考资料:
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/
扩展阅读:admindocs的使用
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/admindocs/