定义
内容发布的部分由网站的管理员负责查看、添加、修改、删除数据,开发这些重复的功能是一件单调乏味、缺乏创造力的工作,为此,Django能够根据定义的模型类自动地生成管理模块
在Django项目中默认启用Admin管理站点
-
创建管理员的用户名和密码
python manage.py createsuperuser
-
按提示填写用户名、邮箱、密码
模型类代码
from django.db import models
class AreaInfo(models.Model):
atitle = models.CharField(verbose_name='标题', max_length=20)
aParent = models.ForeignKey('self', null=True, blank=True)
def title(self):
return self.atitle.encode('utf-8')
title.admin_order_field = 'atitle'
title.short_description = '当前地区名称'
def parent(self):
if self.aParent:
return self.aParent.atitle.encode('utf-8')
else:
return ''
parent.short_description = '父级地区名称'
def __str__(self):
return self.atitle.encode('utf-8')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
admin管理代码
class AreaStackedInline(admin.StackedInline):
model = AreaInfo
extra = 2
class AreaTabularInline(admin.TabularInline):
model = AreaInfo
extra = 2
@admin.register(AreaInfo)
class AreaInfoAdmin(admin.ModelAdmin):
list_per_page = 10
actions_on_bottom = True
actions_on_top = False
list_display = ['id', 'atitle', 'title', 'parent']
list_filter = ['atitle']
search_fields = ['atitle']
fieldsets = [
('基本', {'fields':['atitle']}),
('高级', {'fields':['aParent']})
]
inlines = [AreaTabularInline,]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50