CRM项目知识预备
用户自定义配置与默认配置
如果用户在setting中配置了自己的选项了,那么global_setting中相同的配置项旧被弃用了
init.py
import importlib
from lib.conf import global_settings
import os
class Settings(object):
def __init__(self):
for name in dir(global_settings):
# print(dir(global_settings))
if name.isupper():
k = name
v = getattr(global_settings,name)
setattr(self,k,v) # 将项目默认的配置文件中所有的配置以键值对的形式存入到了settings对象中
# 从全局的大字典中获取暴露给用户的settings文件路径
module_path = os.environ.get('xxx')
module = importlib.import_module(module_path)
for name in dir(module):
if name.isupper():
k = name
v = getattr(module,name)
setattr(self,k,v) # 将暴露给用户的配置文件中所有的配置以键值对的形式存入到了settings对象中
settings = Settings()
start.py
import os
import sys
BASE_DIR = os.path.dirname(__file__)
sys.path.append(BASE_DIR)
# 项目启动 设置全局大字典{'xxx':'conf.settings'}
os.environ.setdefault('xxx','conf.settings')
from lib.conf import settings
print(settings.NAME)
admin五大配置参数
如下图
from django.contrib import admin
from app01 import models
# Register your models here.
# print('from app01 admin')
class BookConfig(admin.ModelAdmin):
list_display = ['title','price','publish_date','publish']
list_display_links = ['title','price']
search_fields = ['title','price']
list_filter = ['publish','authors']
def patch_init(self,request,queryset):
queryset.update(price=666)
patch_init.short_description = '价格批量修改'
actions = [patch_init]
admin.site.register(models.Book,BookConfig)
admin.site.register(models.Publish)
admin.site.register(models.Author)
admin.site.register(models.AuthorDetail)
print(admin.site._registry)
批量插入数据
obj_list = []
for i in range(10000):
obj_list.append(models.Publish(name='出版社%s'%i))
models.Publish.objects.bulik_create(obj_list) #通过先产生对象,然后再通过bulik_create来添加数据
保存上一次的url
import copy
params = copy.deepcopy(request.GET)
params['xxx] = 123 # 将每个url后都添加xxx=123
params.urlencode() # 将字典变成urlencode数据格式即xxx=123&qqq=456的形式
Q查询
1.直接用Q去查
2. from app01 import models
models.Book.objects.filter(title__contains='三',price__contains=2)
<QuerySet []>
from django.db.models import Q
models.Book.objects.filter(Q(title__contains='三')|Q(price__contains=2))
<QuerySet [<Book: 三国演义>]>
models.Book.objects.filter(Q(title__contains='三')|Q(price__contains=8))
<QuerySet [<Book: 金xx>, <Book: 三国演义>]>
q = Q()
q.children.append(('title__contains','三')) # 添加查询条件
q.children.append(('price__contains',8))
models.Book.objects.filter(q)
<QuerySet [<Book: 三国演义>]>
q.connector = 'or' # q对象默认也是and关系 但是可以通过connector修改成or的关系
models.Book.objects.filter(q)
<QuerySet [<Book: 金xx>, <Book: 三国演义>]>
# 实际应用
key_word = request.GET.get('search')
self.key_word = ''
if key_word:
self.key_word = key_word
q = Q()
q.connector = 'or'
for search_field in self.search_fields:
q.children.append(('%s__icontains' % search_field,key_word))
queryset = queryset.filter(q)
父子页面
打开子页面
父页面script
function WindowOpen(url) {
window.open(url,'','width=800px,height=400px left=200px top=100px')
}
子页面调用父页面方法
父方法
function addoption(pop_back_id,pk,text) {
var opEle = document.createElement('option');
opEle.innerText = text;
opEle.value = pk;
opEle.selected = 'selected';
var seEle = document.getElementById(pop_back_id);
seEle.appendChild(opEle)
}
子调父方法
window.opener.addoption('{{ pop_back_id }}',{{ pk }},'{{ text }}');
window.close()
后台
pop_back_id = request.GET.get('pop_back_id')
if form_obj.is_valid():
obj = form_obj.save()
if pop_back_id: #通过前端返回的数据告诉后台是子页面还是不带参数提交的数据的数据
pk = obj.pk
text = str(obj)
return render(request,'stark/pop.html',locals())
else:
return redirect(self.get_reverse_url('list'))
from django.forms.models import ModelChoiceField
for foo in form_obj:
if isinstance(foo.field,ModelChoiceField):
foo.is_pop = True #告诉前端该字段是否是外键
rel_model = self.model._meta.get_field(foo.name).rel.to #获得字段所对应的表名
rel_app_label = rel_model._meta.app_label
rel_model_name = rel_model._meta.model_name
url = reverse('%s_%s_%s' % (rel_app_label,rel_model_name,'add')) #拼接子页面的url
url = url + '?pop_back_id=%s' % foo.auto_id #拼接子页面的后缀参数
foo.url = url
return render(request, 'stark/add_view.html', locals())