13.在xadmin里注册数据表

和在django的admin里注册差不多。也有不一样的。比如先注册users这个app。在app目录下创建adminx.py,

import xadmin

from .models import EmailVerifyRecord
from .models import Banner

class EmailVerifyRecordAdmin(object):
    list_display = ['code','email','send_type','send_time']
    search_fields = ['code','email','send_type']
    list_filter = ['code','email','send_type','send_time']

class BannerAdmin(object):
    list_display = ['title','image','url','index','add_time']
    search_fields = ['title','image','url','index']
    list_filter = ['title','image','url','index','add_time']


xadmin.site.register(EmailVerifyRecord,EmailVerifyRecordAdmin)
xadmin.site.register(Banner,BannerAdmin)

其中import xadmin是引入xadmin的源码,因为之前已经在settings.py里把源码文件夹所在的extra_apps和根目录链接,因此可直接import。.models代表同一目录下的models。创建两个类,命名习惯是在要加入xadmin管理的类后加上Admin。list_display指的是在xadmin上显示的字段,search_fields是可以在哪些字段里搜索关键字,不能有时间。 list_filter是筛选,这个可以筛选时间,方便。最后要注册下xadmin.site.register(EmailVerifyRecord,EmailVerifyRecordAdmin)
意思大概是告诉xadmin表类和管理类的对应关系。至于UserProfile类,大概是由于在settings.py里的AUTH_USER_MODEL = "users.UserProfile",xadmin自动注册了。然后要注册其他app的models!!!复制粘贴!!!
courses的adminx.py

import xadmin
from .models import Course, Lesson, Video, CoursesResources

class CourseAdmin(object):
    list_display = ['name', 'desc', 'detail', 'degree', 'learn_times','students','fav_nums','image','click_nums','add_time']
    search_fields = ['name', 'desc', 'detail', 'degree', 'learn_times','students','fav_nums','image','click_nums']
    list_filter = ['name', 'desc', 'detail', 'degree', 'learn_times','students','fav_nums','image','click_nums','add_time']

class LessonAdmin(object):
    list_display =['name', 'course', 'add_time']
    search_fields = ['name', 'course__name']
    list_filter = ['name', 'course__name', 'add_time']

class VideoAdmin(object):
    list_display = ['lesson', 'name', 'add_time']
    search_fields = ['lesson', 'name']
    list_filter = ['lesson', 'name', 'add_time']

class CoursesResourcesAdmin(object):
    list_display = ['course', 'name','download', 'add_time']
    search_fields = ['course', 'name','download']
    list_filter = ['course', 'name','download', 'add_time']

xadmin.site.register(Course,CourseAdmin)
xadmin.site.register(Lesson,LessonAdmin)
xadmin.site.register(Video,VideoAdmin)
xadmin.site.register(CoursesResources,CoursesResourcesAdmin)

organization的:

import xadmin

from .models import CityDict, CourseOrg, Teacher

class CityDictAdmin(object):
    list_display = ['name', 'desc', 'add_time']
    search_fields = ['name', 'desc']
    list_filter = ['name', 'desc', 'add_time']

class CourseOrgAdmin(object):
    list_display = ['name', 'desc', 'click_nums','fav_nums','image','address','city']
    search_fields = ['name', 'desc', 'click_nums','fav_nums','image','address','city']
    list_filter = ['name', 'desc', 'click_nums','fav_nums','image','address','city']

class TeacherAdmin(object):
    list_display = ['org', 'name', 'work_years','work_company','work_position','points','click_nums','fav_nums','add_time']
    search_fields = ['org', 'name', 'work_years','work_company','work_position','points','click_nums','fav_nums']
    list_filter = ['org', 'name', 'work_years','work_company','work_position','points','click_nums','fav_nums','add_time']

xadmin.site.register(CityDict,CityDictAdmin)
xadmin.site.register(CourseOrg,CourseOrgAdmin)
xadmin.site.register(Teacher,TeacherAdmin)

operation的:

import xadmin

from .models import UserAsk, CourseComments, UserFavorite, UserMessage, UserCourse

class UserAskAdmin(object):
    list_display = ['name', 'mobile', 'course_name', 'add_time']
    search_fields = ['name', 'mobile', 'course_name']
    list_filter = ['name', 'mobile', 'course_name', 'add_time']


class CourseCommentsAdmin(object):
    list_display = ['user', 'course', 'comments', 'add_time']
    search_fields = ['user', 'course', 'comments']
    list_filter = ['user', 'course', 'comments', 'add_time']

class UserFavoriteAdmin(object):
    list_display = ['user', 'fav_id', 'fav_type', 'add_time']
    search_fields = ['user', 'fav_id', 'fav_type']
    list_filter = ['user', 'fav_id', 'fav_type', 'add_time']

class UserMessageAdmin(object):
    list_display = ['user', 'message', 'has_read', 'add_time']
    search_fields = ['user', 'message', 'has_read']
    list_filter = ['user', 'message', 'has_read', 'add_time']


class UserCourseAdmin(object):
    list_display = ['user', 'course',  'add_time']
    search_fields = ['user', 'course']
    list_filter = ['user', 'course',  'add_time']


xadmin.site.register(UserAsk,UserAskAdmin)
xadmin.site.register(CourseComments,CourseCommentsAdmin)
xadmin.site.register(UserFavorite,UserFavoriteAdmin)
xadmin.site.register(UserMessage,UserMessageAdmin)
xadmin.site.register(UserCourse,UserCourseAdmin)

这么多东西具体原理见前面users的部分。现在如果重启项目再进入xadmin界面可以看到一堆自己注册进去的表了!!!

你可能感兴趣的:(13.在xadmin里注册数据表)