1、扩展auth_user
from django.contrib.auth.models import User,AbstractUser
class UserInfo(AbstractUser):
tel = models.CharField(max_length=32) setting配置
AUTH_USER_MODEL = 'app01.UserInfo'
1.根据str,拿字段对象,取中文
val = self.model._meta.get_field(field).verbose_name 2.模型名称,app名称
model_name = self.model._meta.model_name
app_lable = self.model._meta.app_label 3.判断是否可调用
if callable(field):
val = field(self, header = True) 4.字符串找对象得属性,反射
val = getattr(data, field) 5.返回标签
from django.utils.safestring import mark_safe
return mark_safe("<input id='choice' type='checkbox'>") 6.插入数组[]
temp.append(ModelStark.checkbox)
temp.extend(self.list_display) 7.反向解析,含正则
_url = reverse("%s_%s_delete" % (app_label, model_name), args=(obj.pk,))
_url = reverse("%s_%s_add" % (app_label, model_name)) 8.复选框
$('#choice').click(function () {
if($(this).prop('checked')){
$('.choice_item').prop('checked',true)
}else{
$('.choice_item').prop('checked',false)
}
})
1.判断用户是否配置,配置才显示search框, get查询
search_fields = ['title', 'price'] 显示key_words{{ showlist.config.key_words }}
{% if showlist.config.search_fields %}
<form action="" class="pull-right">
<input type="text" name="q" value="{{ showlist.config.key_words }}">
<button>submit</button>
</form>
{% endif %} 2.Q对象
# 获取search得Q对象
search_connection = self.get_search_condition(request) # print("connection:",search_connection)
# connection: (or: ('title__contains', '3'), ('price__contains', '3')) # 筛选当前表得所有数据
data_list = self.model.objects.all().filter(search_connection) 3.Q查询
两种方式,一种可以放str,就是下面这种,一种放字段; def get_search_condition(self, request):
key_words = request.GET.get('q', "")
self.key_words = key_words # self.search_fields ['title','price'] from django.db.models import Q
search_connection = Q()
if key_words:
search_connection.connector = "or"
for search_field in self.search_fields:
search_connection.children.append((search_field+"__contains", key_words)) return search_connection 4.模糊查询
(title__contains)(price__contains)
search_connection.children.append((search_field+"__contains", key_words))