1,querydict用法:
from django.http import QueryDict
def edit_link_tag(self,pk,text):
query_str = self.request.GET.urlencode() # page=2&nid=1
params = QueryDict(mutable=True) #mutable=True让Querydict可以变化
params[self.config._query_param_key] = query_str
return mark_safe(‘<a href="%s?%s">%s</a>‘ % (self.config.get_change_url(pk), params.urlencode(),text,)) # /stark/app01/userinfo/
2,类里面的迭代功能:def __iter__(self):
3,在JSON传到html页面的时候,中文也可以显示
ensure_ascii=False
4,通过对象反向找与之关联类名以及所有对象
from django.db.models.fields.reverse_related import ManyToOneRel
if form.is_valid():
result = {‘status‘: False, ‘obj_pk‘: None, ‘obj_name‘: None, ‘popbackid‘: None}
form_obj=form.save()
if _popbackid: #如果add里的popup触发,点击提交后 这里代表已经有popup
model_name=request.GET.get(‘model_name‘)
related_name=request.GET.get(‘related_name‘)
for re_name in form_obj._meta.related_objects: #通过这个对象反向找到与之相关联的对象
form_model_name=re_name.field.model._meta.model_name #通过这个对象找到类名
_related_name=re_name.related_name
if type(re_name)==ManyToOneRel:
field_name=re_name.field_name
else:
field_name=‘pk‘
limit_choice_to=re_name.limit_choices_to
if model_name==form_model_name and related_name==str(_related_name):
is_exists=self.models_class.objects.filter(**limit_choice_to,pk=form_obj.pk).exists()
if is_exists:
result[‘status‘] = True
result[‘obj_name‘] = str(form_obj)
result[‘obj_pk‘] = getattr(form_obj,field_name)
result[‘popbackid‘]=_popbackid
return render(request,‘stark/popresponse.html‘,{‘result‘:json.dumps(result,ensure_ascii=False)})
5,通过Q把类里的字段一起判断,结果是一个键值对
con=Q() con.connector=‘or‘ #这里的or是什么 if seach_name: for file in seach_file: con.children.append((file,seach_name))
Q对象OR连接
def public(self,request): time_now=datetime.datetime.now().now() #取到现在的时间 no_deal=time_now-datetime.timedelta(days=15) no_talk=time_now-datetime.timedelta(days=3) con1=Q() con1.connector=‘OR‘ #里面的数据用or连接 con1.children.append((‘last_consult_date__lt‘,no_talk)) con1.children.append((‘recv_date__lt‘,no_deal)) # con2=Q() # con2.children.append((‘status‘,2)) # con=Q() # con.add(con1,‘AND‘) # con.add(con2,‘AND‘) cust_list=models.Customer.objects.filter(con1,status=2) #这个的Q对象con1 和status是and连接,但是con1里面是or连接.