xadmin中注册自己的Model
- 在app中新建
adminx.py
- 编辑文件
import xadmin
from apps.courses.models import Course
# xadmin允许我们不继承任何东西
class CourseAdmin(object):
pass
xadmin.site.register(Course, CourseAdmin)
- app显示中文
修改apps.py
from django.apps import AppConfig
class CoursesConfig(AppConfig):
name = 'apps.courses'
verbose_name = '课程管理'
- 添加课程信息,需要添加老师+机构+城市的信息(老师里面也需要先添加机构信息,机构信息依赖城市信息)
- 把机构app也注册进来(老师+机构+城市)
修改 apps/organizations/adminx.py
import xadmin
from apps.organizations.models import Teacher, CourseOrg, City
# xadmin允许我们不继承任何东西
class TeacherAdmin(object):
pass
class CourseOrgAdmin(object):
pass
class CityAdmin(object):
pass
xadmin.site.register(Teacher, TeacherAdmin)
xadmin.site.register(CourseOrg, CourseOrgAdmin)
xadmin.site.register(City, CityAdmin)
- 添加城市信息,返回的信息看不懂,所以要重写__str__方法
- 修改机构models
from django.db import models
from apps.users.models import BaseModel
class City(BaseModel):
name = models.CharField(max_length=20, verbose_name='城市')
desc = models.CharField(max_length=200, verbose_name='描述')
class Meta:
verbose_name = '城市'
verbose_name_plural = verbose_name
def __str__(self):
return self.name
class CourseOrg(BaseModel):
name = models.CharField(max_length=50, verbose_name='机构名称')
desc = models.TextField(verbose_name='描述')
tag = models.CharField(default='全国知名', max_length=10, verbose_name='机构标签')
category = models.CharField(default='pxjp', verbose_name='机构类别', max_length=4,
choices=(('pxjg', '培训机构'), ('gr', '个人'), ('gx', '高校')))
click_nums = models.IntegerField(default=0, verbose_name='点击数')
fav_nums = models.IntegerField(default=0, verbose_name='收藏数')
image = models.ImageField(upload_to='org/%Y/%m', verbose_name='logo', max_length=100)
address = models.CharField(max_length=150, verbose_name="机构地址")
students = models.IntegerField(default=0, verbose_name="学习人数")
course_nums = models.IntegerField(default=0, verbose_name="课程数")
city = models.ForeignKey(City, on_delete=models.CASCADE, verbose_name='所在城市')
class Meta:
verbose_name = '课程机构'
verbose_name_plural = verbose_name
def __str__(self):
return self.name
class Teacher(BaseModel):
org = models.ForeignKey(CourseOrg, on_delete=models.CASCADE, verbose_name="所属机构")
name = models.CharField(max_length=50, verbose_name="教师名")
work_years = models.IntegerField(default=0, verbose_name="工作年限")
work_company = models.CharField(max_length=50, verbose_name="就职公司")
work_position = models.CharField(max_length=50, verbose_name="公司职位")
points = models.CharField(max_length=50, verbose_name="教学特点")
click_nums = models.IntegerField(default=0, verbose_name="点击数") # 人气排序用
fav_nums = models.IntegerField(default=0, verbose_name="收藏数")
age = models.IntegerField(default=18, verbose_name="年龄")
image = models.ImageField(upload_to="teacher/%Y/%m", verbose_name="头像", max_length=100)
class Meta:
verbose_name = '教师'
verbose_name_plural = verbose_name
def __str__(self):
return self.name
现在显示就正常了
- 定制页面的功能
1.显示列内容 list_display
class CityAdmin(object):
list_display = ['id', 'name', 'desc']
2.搜索的字段 search_fields
class CityAdmin(object):
list_display = ['id', 'name', 'desc']
search_fields = ['name', 'desc']
3.过滤的字段 list_filter
class CityAdmin(object):
list_display = ['id', 'name', 'desc']
search_fields = ['name', 'desc']
list_filter = ['name', 'desc', 'add_time']
4.在列表页直接修改字段 list_editable
数据很大的时候这个就显的很方便了
class CityAdmin(object):
list_display = ['id', 'name', 'desc']
search_fields = ['name', 'desc']
list_filter = ['name', 'desc', 'add_time']
list_editable = ['name', 'desc']
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。