目录
[TOC]
判断字段值是否包含某个数字,如果是字符,建议用icontains
Entry.objects.filter(Q(id__contains='1') & Q(id__contains='2') & Q(id__contains='3'))
Entry.objects.filter(Q(id__contains=1) & Q(id__contains=2) & Q(id__contains=3))
Entry.objects.filter(reduce(Q(id__contains=x) for x in [1, 2, 3]))
管理员身份运行cmd配置虚拟环境
- mkvirtualenv youpinmodel
- pip install django==1.9.8
- 下载python的mysql驱动,进入到文件目录cd /d C:\Users\Administrator\Desktop
pip install mysql(tab) - 在虚拟环境youpinmodel中新建项目
- pip install pillow
根据项目需求设计app
- users-用户管理
- employee-考核对象管理
- institution-制度管理
- informationinput-考核信息录入
配置数据库连接
#数据库连接配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': "youpin1",
'USER':'root',
'PASSWORD':'',
'HOST':'127.0.0.1'
}
}
新建users app
新建users app
startapp users
model配置
#载入utf8编码
# _*_ encoding:utf-8 _*_
#继承app
from django.contrib.auth.models import AbstractUser
#新建模型,CharField要定义max_length
class UserProfile(AbstractUser):
nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default="")
birday = models.DateField(verbose_name=u"生日", null=True, blank=True)
gender = models.CharField(max_length=6, choices=(("male",u"男"),("female","女")), default="female")
image = models.ImageField(upload_to="image/%Y/%m",default=u"image/default.png", max_length=100)
user_id = models.CharField(max_length=11, null=True, blank=True)
class Meta:
verbose_name = "用户信息"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.username
将UserProfile注册到app中,并重载setting方法
此步前一定要restart一下,否则后面xadmin就要重新生成auth_user,或者注释掉下面一句重新生成下表
AUTH_USER_MODEL = "users.UserProfile"
生成user表
makemigrations users
migrate users
新建剩余app
考核对象表
class w_section(models.Model):
w_sectionid = models.CharField(max_length=2, verbose_name=u"工段ID")
name = models.CharField(max_length=50, verbose_name=u"工段名")
class Meta:
verbose_name = u"工段"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.name
class w_team(models.Model):
w_teamid = models.CharField(max_length=4, verbose_name=u"车间班组ID",default=0)
name = models.CharField(max_length=50, verbose_name=u"车间班组名")
class Meta:
verbose_name = u"车间班组"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.name
class w_employee(models.Model):
w_employeeid = models.CharField(max_length=10, verbose_name=u"员工编号",default=0)
name = models.CharField(max_length=50, verbose_name=u"员工姓名")
w_sectionid = models.ForeignKey(w_section, verbose_name=u"所属工段")
w_teamid = models.ForeignKey(w_team, verbose_name=u"所属车间班组")
class Meta:
verbose_name = u"员工信息管理"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.name
制度表
class inscategory(models.Model):
categoryid = models.CharField(max_length=2, verbose_name=u"制度类别ID")
category = models.CharField(max_length=50, verbose_name=u"制度类别")
class Meta:
verbose_name = u"制度分类"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.category
class institution(models.Model):
categoryid = models.ForeignKey(inscategory, verbose_name=u"制度类别ID")
institution = models.CharField(max_length=50, verbose_name=u"制度内容")
institutionid = models.CharField(max_length=10, verbose_name=u"制度ID")
score = models.CharField(max_length=2, verbose_name=u"加扣分")
class Meta:
verbose_name = u"制度"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.institutionid
考核信息录入,操作上面两张表
class informationinput(models.Model):
"考核信息录入"
user=models.ForeignKey(UserProfile,verbose_name=u"考核员")
category=models.ForeignKey(institution,verbose_name="制度ID和加扣分")
sketch=models.CharField(max_length=500,verbose_name=u"简述")
w_employeeid = models.ForeignKey(w_employee, verbose_name=u"考核对象")
add_time=models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
image = models.ImageField(upload_to="information/%y/%m", default=u"information/default.png", max_length=100)
has_agree=models.BooleanField(default=False,verbose_name=u"是否审查通过")
class Meta:
verbose_name = u"考核信息"
verbose_name_plural = verbose_name
将所有app放到apps文件夹下
import sys
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
python manage.py runserver 0.0.0.0:8000
后台管理配置
createsuperuser
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False
#使用本地时间
安装xadmin
pip install xadmin
#注册app
'xadmin',
'crispy_forms',
#url注册
import xadmin
url(r'^xadmin/', xadmin.site.urls),
#同步表
makemigrations
migrate
#移动xadmin到extra_apps中,并mark
sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
pip uninstall xadmin
makemigrations xadmin
migrate xadmin
设置模板,注册其它app
# -*- coding: utf-8 -*-
__author__ = 'grl'
__date__ = '$DATE $TIME'
#制度app
import xadmin
from .models import inscategory,institution
class inscategoryAdmin(object):
list_display = ['categoryid', 'category']
search_fields = ['categoryid', 'category']
list_filter = ['categoryid', 'category']
class institutionAdmin(object):
list_display = ['categoryid', 'institution','institutionid','score']
search_fields = ['categoryid', 'institution','institutionid','score']
list_filter = ['categoryid', 'institution','institutionid','score']
xadmin.site.register(inscategory, inscategoryAdmin)
xadmin.site.register(institution, institutionAdmin)
#员工app
import xadmin
from .models import w_section,w_team,w_employee
class w_sectionAdmin(object):
list_display = ['w_sectionid', 'name']
search_fields = ['w_sectionid', 'name']
list_filter = ['w_sectionid', 'name']
class w_teamAdmin(object):
list_display = ['w_teamid', 'name']
search_fields = ['w_teamid', 'name']
list_filter = ['w_teamid', 'name']
class w_employeeAdmin(object):
list_display = ['w_employeeid', 'name','w_sectionid','w_teamid']
search_fields = ['w_employeeid', 'name','w_sectionid','w_teamid']
list_filter = ['w_employeeid', 'name','w_sectionid','w_teamid']
xadmin.site.register(w_section, w_sectionAdmin)
xadmin.site.register(w_team, w_teamAdmin)
xadmin.site.register(w_employee, w_employeeAdmin)
#考核信息录入app
import xadmin
from .models import informationinput
class informationinputAdmin(object):
list_display = ['user', 'category','sketch','w_employeeid','add_time','image','has_agree']
search_fields = ['user', 'category','sketch','w_employeeid','add_time','image','has_agree']
list_filter = ['user', 'category','sketch','w_employeeid','add_time','image','has_agree']
xadmin.site.register(informationinput, informationinputAdmin)
#用户adminx中配置全站基础
import xadmin
from xadmin import views
class BaseSetting(object):
enable_themes = True
use_bootswatch = True
class GlobalSettings(object):
site_title = "信息后台管理系统"
site_footer = ""
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)
#apps.py中配置中文显示
# -*- coding: utf-8 -*-
verbose_name = u"制度管理"
#_init_文件中配置
default_app_config = "institution.apps.InstitutionConfig"
问题总结
- 获取当前用户姓名:
重载save_models方法 直接在方法中通过self.request.user就可以获取到当前操作xadmin的用户了
- 外键过滤
def get_context(self):
context = superCourseAdmin, self).get_context()
if 'form' in context:
context['form'].fields['course_org'].queryset = CourseOrg.objects.filter(is_main_stage=True)
return context
#在adminx里面重载这个函数,在xadmin中可以直接配置外键过滤 比如course的teacher是外键 如果要通过teacher的name过滤 直接在filter中写 "teahcer__name"就行了, 注意是双下划线
- 设置mysql只能本地访问
配置mysql的bind-addr 只需要配置成127.0.0.1
获取用户名等
- 获取当前用户姓名:
重载save_models方法 直接在方法中通过self.request.user就可以获取到当前操作xadmin的用户了
- 外键过滤
def get_context(self):
context = superCourseAdmin, self).get_context()
if 'form' in context:
context['form'].fields['course_org'].queryset = CourseOrg.objects.filter(is_main_stage=True)
return context
#在adminx里面重载这个函数,在xadmin中可以直接配置外键过滤 比如course的teacher是外键 如果要通过teacher的name过滤 直接在filter中写 "teahcer__name"就行了, 注意是双下划线
- 设置mysql只能本地访问
配置mysql的bind-addr 只需要配置成127.0.0.1
管理员身份运行cmd配置虚拟环境
- mkvirtualenv youpinmodel
- pip install django==1.9.8
- 下载python的mysql驱动,进入到文件目录cd /d C:\Users\Administrator\Desktop
pip install mysql(tab) - 在虚拟环境youpinmodel中新建项目
- pip install pillow
根据项目需求设计app
- users-用户管理
- employee-考核对象管理
- institution-制度管理
- informationinput-考核信息录入
配置数据库连接
#数据库连接配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': "youpin1",
'USER':'root',
'PASSWORD':'',
'HOST':'127.0.0.1'
}
}
新建users app
新建users app
startapp users
model配置
#载入utf8编码
# _*_ encoding:utf-8 _*_
#继承app
from django.contrib.auth.models import AbstractUser
#新建模型,CharField要定义max_length
class UserProfile(AbstractUser):
nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default="")
birday = models.DateField(verbose_name=u"生日", null=True, blank=True)
gender = models.CharField(max_length=6, choices=(("male",u"男"),("female","女")), default="female")
image = models.ImageField(upload_to="image/%Y/%m",default=u"image/default.png", max_length=100)
user_id = models.CharField(max_length=11, null=True, blank=True)
class Meta:
verbose_name = "用户信息"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.username
将UserProfile注册到app中,并重载setting方法
此步前一定要restart一下,否则后面xadmin就要重新生成auth_user,或者注释掉下面一句重新生成下表
AUTH_USER_MODEL = "users.UserProfile"
生成user表
makemigrations users
migrate users
新建剩余app
考核对象表
class w_section(models.Model):
w_sectionid = models.CharField(max_length=2, verbose_name=u"工段ID")
name = models.CharField(max_length=50, verbose_name=u"工段名")
class Meta:
verbose_name = u"工段"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.name
class w_team(models.Model):
w_teamid = models.CharField(max_length=4, verbose_name=u"车间班组ID",default=0)
name = models.CharField(max_length=50, verbose_name=u"车间班组名")
class Meta:
verbose_name = u"车间班组"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.name
class w_employee(models.Model):
w_employeeid = models.CharField(max_length=10, verbose_name=u"员工编号",default=0)
name = models.CharField(max_length=50, verbose_name=u"员工姓名")
w_sectionid = models.ForeignKey(w_section, verbose_name=u"所属工段")
w_teamid = models.ForeignKey(w_team, verbose_name=u"所属车间班组")
class Meta:
verbose_name = u"员工信息管理"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.name
制度表
class inscategory(models.Model):
categoryid = models.CharField(max_length=2, verbose_name=u"制度类别ID")
category = models.CharField(max_length=50, verbose_name=u"制度类别")
class Meta:
verbose_name = u"制度分类"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.category
class institution(models.Model):
categoryid = models.ForeignKey(inscategory, verbose_name=u"制度类别ID")
institution = models.CharField(max_length=50, verbose_name=u"制度内容")
institutionid = models.CharField(max_length=10, verbose_name=u"制度ID")
score = models.CharField(max_length=2, verbose_name=u"加扣分")
class Meta:
verbose_name = u"制度"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.institutionid
考核信息录入,操作上面两张表
class informationinput(models.Model):
"考核信息录入"
user=models.ForeignKey(UserProfile,verbose_name=u"考核员")
category=models.ForeignKey(institution,verbose_name="制度ID和加扣分")
sketch=models.CharField(max_length=500,verbose_name=u"简述")
w_employeeid = models.ForeignKey(w_employee, verbose_name=u"考核对象")
add_time=models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
image = models.ImageField(upload_to="information/%y/%m", default=u"information/default.png", max_length=100)
has_agree=models.BooleanField(default=False,verbose_name=u"是否审查通过")
class Meta:
verbose_name = u"考核信息"
verbose_name_plural = verbose_name
将所有app放到apps文件夹下
import sys
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
python manage.py runserver 0.0.0.0:8000
后台管理配置
createsuperuser
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False
#使用本地时间
安装xadmin
pip install xadmin
#注册app
'xadmin',
'crispy_forms',
#url注册
import xadmin
url(r'^xadmin/', xadmin.site.urls),
#同步表
makemigrations
migrate
#移动xadmin到extra_apps中,并mark
sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
pip uninstall xadmin
makemigrations xadmin
migrate xadmin
设置模板,注册其它app
# -*- coding: utf-8 -*-
__author__ = 'grl'
__date__ = '$DATE $TIME'
#制度app
import xadmin
from .models import inscategory,institution
class inscategoryAdmin(object):
list_display = ['categoryid', 'category']
search_fields = ['categoryid', 'category']
list_filter = ['categoryid', 'category']
class institutionAdmin(object):
list_display = ['categoryid', 'institution','institutionid','score']
search_fields = ['categoryid', 'institution','institutionid','score']
list_filter = ['categoryid', 'institution','institutionid','score']
xadmin.site.register(inscategory, inscategoryAdmin)
xadmin.site.register(institution, institutionAdmin)
#员工app
import xadmin
from .models import w_section,w_team,w_employee
class w_sectionAdmin(object):
list_display = ['w_sectionid', 'name']
search_fields = ['w_sectionid', 'name']
list_filter = ['w_sectionid', 'name']
class w_teamAdmin(object):
list_display = ['w_teamid', 'name']
search_fields = ['w_teamid', 'name']
list_filter = ['w_teamid', 'name']
class w_employeeAdmin(object):
list_display = ['w_employeeid', 'name','w_sectionid','w_teamid']
search_fields = ['w_employeeid', 'name','w_sectionid','w_teamid']
list_filter = ['w_employeeid', 'name','w_sectionid','w_teamid']
xadmin.site.register(w_section, w_sectionAdmin)
xadmin.site.register(w_team, w_teamAdmin)
xadmin.site.register(w_employee, w_employeeAdmin)
#考核信息录入app
import xadmin
from .models import informationinput
class informationinputAdmin(object):
list_display = ['user', 'category','sketch','w_employeeid','add_time','image','has_agree']
search_fields = ['user', 'category','sketch','w_employeeid','add_time','image','has_agree']
list_filter = ['user', 'category','sketch','w_employeeid','add_time','image','has_agree']
xadmin.site.register(informationinput, informationinputAdmin)
#用户adminx中配置全站基础
import xadmin
from xadmin import views
class BaseSetting(object):
enable_themes = True
use_bootswatch = True
class GlobalSettings(object):
site_title = "信息后台管理系统"
site_footer = ""
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)
#apps.py中配置中文显示
# -*- coding: utf-8 -*-
verbose_name = u"制度管理"
#_init_文件中配置
default_app_config = "institution.apps.InstitutionConfig"