[py][mx]xadmin详细设置-将app注册到xadmin

首先createsuperuser,创建用户, 然后登陆xadmin.

理解下models的各个字段

  • 复数形式及返回

[py][mx]xadmin详细设置-将app注册到xadmin_第1张图片

  • 注册app
    users/adminx.py

[py][mx]xadmin详细设置-将app注册到xadmin_第2张图片

  • 显示字段
    users/adminx.py
    注: 这里显示的顺序和列表顺序是一致的.

[py][mx]xadmin详细设置-将app注册到xadmin_第3张图片

  • 搜索字段(手动输入关键字搜索)search_fileds
    users/adminx.py

一般不搜索时间,不好搜索. 这里看到还可以将数据导出各种格式.
[py][mx]xadmin详细设置-将app注册到xadmin_第4张图片

到此为止,我们每个表的增删改查功能就齐全了.

  • 过滤器list_filter(自动点选搜索), 可以通过时间来点点点过滤了

[py][mx]xadmin详细设置-将app注册到xadmin_第5张图片

  • 搜索字段和过滤器

[py][mx]xadmin详细设置-将app注册到xadmin_第6张图片

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']

  • 修改app显示

[py][mx]xadmin详细设置-将app注册到xadmin_第7张图片

app/__init__.py
default_app_config = "users.apps.UsersConfig"

users/apps.py
class UsersConfig(AppConfig):
    name = 'users'
    verbose_name = u"用户信息"
  • 一个app下多个模型顺序
    界面上的顺序和注册顺序一致
    [py][mx]xadmin详细设置-将app注册到xadmin_第8张图片

  • 访问外键

course模型
[py][mx]xadmin详细设置-将app注册到xadmin_第9张图片

一个course有多个lesson(章节), 章节里要过滤一些关于course的字段, 需要 course__name键的形式访问才会出现.

[py][mx]xadmin详细设置-将app注册到xadmin_第10张图片

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

代码

在users app下创建adminx.py

import xadmin
from users.models import EmailVerifyRecord, Banner


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


xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin)

courses/adminx.py

#!/usr/bin/env python
# coding=utf-8
import xadmin
from courses.models import Course, Lesson, Video, CourseResourse


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']


xadmin.site.register(Course, CourseAdmin)


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


xadmin.site.register(Lesson, LessonAdmin)


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


xadmin.site.register(Video, VideoAdmin)


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


xadmin.site.register(CourseResourse, CourseResourseAdmin)






courses/__init__.py
default_app_config = "courses.apps.CoursesConfig"


courses/app.py
class CoursesConfig(AppConfig):
    name = 'courses'
    verbose_name = u"课程"

operation/xadmin.py

#!/usr/bin/env python
# coding=utf-8

import xadmin
from operation.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']


xadmin.site.register(UserAsk, UserAskAdmin)


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


xadmin.site.register(CourseComments, CourseCommentsAdmin)


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


xadmin.site.register(UserFavorite, UserFavoriteAdmin)


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']


xadmin.site.register(UserMessage, UserMessageAdmin)


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


xadmin.site.register(UserCourse, UserCourseAdmin)

organization/adminx.py

#!/usr/bin/env python
# coding=utf-8

import xadmin
from organization.models import CityDict, CourseOrg, Teacher


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


xadmin.site.register(CityDict, CityDictAdmin)


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


xadmin.site.register(CourseOrg, CourseOrgAdmin)


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(Teacher, TeacherAdmin)

参考

转载于:https://www.cnblogs.com/iiiiiher/p/8385221.html

你可能感兴趣的:([py][mx]xadmin详细设置-将app注册到xadmin)