一、前言
本Part,将介绍Django-Xadmin的基础录入控件的使用和效果。包括:
1、文本录入
2、数字录入
3、日期时间录入
4、URL录入
5、Email录入
6、IP地址录入
二、目录结构总览
1、本part,我们在这个结构下进行代码填充:
Project 目录 (Django-Xadmin学习项目)
|
|-- app
| `-- practise
...
三、填充代码
1、在project/app/practise/models.py,填充代码:
# -*- coding: utf-8 -*-
from django.db import models
from datetime import datetime
# Create your models here.
class Basic(models.Model):
input_single = models.CharField(max_length=10, verbose_name=u'单行文本录入案例')
input_multi = models.TextField(max_length=100, verbose_name=u'多行文本录入案例', null=True, blank=True,)
input_integer = models.IntegerField(default=0, verbose_name=u'整数录入案例', null=True, blank=True,)
input_positive_integer = models.PositiveIntegerField(verbose_name=u'正整数录入案例', null=True, blank=True,)
input_big_integer = models.BigIntegerField(default=0, verbose_name=u'长整型录入案例', null=True, blank=True,)
input_float = models.FloatField(default=0.0, verbose_name=u'浮点数录入案例', null=True, blank=True,)
# DecimalFields,报了2个错,先注释
# DecimalFields must define a 'decimal_places' attribute.
# DecimalFields must define a 'max_digits' attribute.
# input_decimal = models.DecimalField(default=0.0, verbose_name=u'小数录入案例', null=True, blank=True,)
input_date = models.DateField(verbose_name=u'日期录入案例', null=True, blank=True,)
input_time = models.TimeField(verbose_name=u'时间录入案例', null=True, blank=True,)
input_datetime = models.DateTimeField(default=datetime.now, verbose_name=u'日期时间录入案例', null=True, blank=True,)
# DurationField:
# Uses interval on postgres, INVERAL DAY TO SECOND on Oracle, and bigint of
# microseconds on other databases.
# input_duration = models.DurationField(verbose_name=u'日期期间录入案例', null=True, blank=True,)
input_url = models.URLField(max_length=100, verbose_name=u'URL录入案例', null=True, blank=True,)
input_email = models.EmailField(max_length=500, verbose_name=u'Email录入案例', null=True, blank=True,)
# IPAddressField:报错:IPAddressField has been removed except for support in historical migrations.
# input_ip = models.IPAddressField(max_length=500, verbose_name=u'IP地址录入案例', null=True, blank=True,)
input_generic_ip = models.GenericIPAddressField(max_length=500, verbose_name=u'通用IP地址录入案例', null=True, blank=True,)
input_slug = models.SlugField(max_length=100, verbose_name=u'标头录入案例', null=True, blank=True, )
add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')
class Meta:
verbose_name = u'录入控件一览'
verbose_name_plural = verbose_name
2、在数据库生成新定义的表,Tools 工具栏运行 run manage.py Task输入:
makemigrations practise
migrate practise
3、数据库多了一张class对应的表
四、把这app注册到xadmin后台管理系统
1、在apps\practise\中创建一个adminx.py文件
# -*- coding: utf-8 -*-
import xadmin
from .models import Basic
class BasicAdmin(object):
list_display = ['input_single'] # 控制显示列数
search_fields = ['input_single'] # 控制搜索框的显示
list_filter = ['input_single'] # 控制筛选
xadmin.site.register(Basic, BasicAdmin)
五、查看效果
1、重启项目
2、查看效果