F()可以将数据库里的数字类型的数据,转换为可以数字类型
首先要导入 from django.db.models import F
from django.shortcuts import render from app1.models import * #导入数据库操作模块 from django.db.models import F #导入F类 #逻辑处理模块 def special(request): #获取yu_wen_lao_shi表里id等于1的那条数据,将shuzi字段的值自身加100 yu_wen_lao_shi.objects.filter(id=1).update(shuzi=F(‘shuzi‘) + 100) return render(request,‘index.html‘,locals()) #打开页面
Q()运算符:|或者、&并且
# Q # # 方式一: # Q(nid__gt=10) # Q(nid=8) | Q(nid__gt=10) # Q(Q(nid=8) | Q(nid__gt=10)) & Q(caption=‘root‘) # 方式二: # con = Q() # q1 = Q() # q1.connector = ‘OR‘ # q1.children.append((‘id‘, 1)) # q1.children.append((‘id‘, 10)) # q1.children.append((‘id‘, 9)) # q2 = Q() # q2.connector = ‘OR‘ # q2.children.append((‘c1‘, 1)) # q2.children.append((‘c1‘, 10)) # q2.children.append((‘c1‘, 9)) # con.add(q1, ‘AND‘) # con.add(q2, ‘AND‘) # # models.Tb1.objects.filter(con)
实列:
from django.shortcuts import render from app1.models import * #导入数据库操作模块 from django.db.models import F,Q #导入F和Q from django.db.models import Count, Min, Max, Sum #逻辑处理模块 def special(request): # Q(nid__gt=10) a = yu_wen_lao_shi.objects.filter(Q(id__gt=3)).values() #获取id大于3的数据 print(a) #Q(nid=8) | Q(nid__gt=10) b = yu_wen_lao_shi.objects.filter(Q(nl=20) | Q(nl=22)).values() #获取nl字段等于20,或者nl字段等于22的数据 print(b) c = yu_wen_lao_shi.objects.filter(Q(id__gt=2) & Q(nl=21)).values() #获取id大于2并且nl等于21的数据 print(c) return render(request,‘index.html‘,locals()) #打开页面
执行原生SQL
# from django.db import connection, connections # cursor = connection.cursor() # cursor = connections[‘default‘].cursor() # cursor.execute("""SELECT * from auth_user where id = %s""", [1]) # row = cursor.fetchone()
queryset对象序列化
django的json类型模块serialize()
from django.shortcuts import render from app1.models import * #导入数据库操作模块 from django.db.models import F,Q #导入F和Q from django.core import serializers #django的将queryset对象转换成json类型模块 import json #逻辑处理模块 def special(request): # Q(nid__gt=10) a = yu_wen_lao_shi.objects.filter(Q(id__gt=3)) #获取id大于3的数据 print(a) #得到queryset对象 b = serializers.serialize("json",a) #将queryset对象转换成json类型 print(b) #得到json类型 c = json.loads(b) #将json类型还原成Python数据类型 print(c) return render(request,‘index.html‘,locals()) #打开页面 # 返回: # <QuerySet [<yu_wen_lao_shi: 张三4>, <yu_wen_lao_shi: 张三5>]> # [{"model": "app1.yu_wen_lao_shi", "pk": 4, "fields": {"xm": "\u5f20\u4e094", "xb": "\u5973", "nl": "21", "shg": "174", "gz": "1500"}}, {"model": "app1.yu_wen_lao_shi", "pk": 5, "fields": {"xm": "\u5f20\u4e095", "xb": "\u5973", "nl": "23", "shg": "175", "gz": "1500"}}] # [{‘pk‘: 4, ‘fields‘: {‘xb‘: ‘女‘, ‘shg‘: ‘174‘, ‘gz‘: ‘1500‘, ‘xm‘: ‘张三4‘, ‘nl‘: ‘21‘}, ‘model‘: ‘app1.yu_wen_lao_shi‘}, {‘pk‘: 5, ‘fields‘: {‘xb‘: ‘女‘, ‘shg‘: ‘175‘, ‘gz‘: ‘1500‘, ‘xm‘: ‘张三5‘, ‘nl‘: ‘23‘}, ‘model‘: ‘app1.yu_wen_lao_shi‘}]
list()将queryset对象转换成列表
from django.shortcuts import render from app1.models import * #导入数据库操作模块 from django.db.models import F,Q #导入F和Q from django.core import serializers #django的将queryset对象转换成json类型模块 import json #逻辑处理模块 def special(request): # Q(nid__gt=10) a = yu_wen_lao_shi.objects.filter(Q(id__gt=3)).values() #获取id大于3的数据 print(a) #得到queryset对象 b = list(a) #将queryset对象转换成列表 print(b) return render(request, ‘index.html‘, locals()) # 打开页面 # 返回: # <QuerySet [{‘xb‘: ‘女‘, ‘gz‘: ‘1500‘, ‘id‘: 4, ‘xm‘: ‘张三4‘, ‘nl‘: ‘21‘, ‘shg‘: ‘174‘}, {‘xb‘: ‘女‘, ‘gz‘: ‘1500‘, ‘id‘: 5, ‘xm‘: ‘张三5‘, ‘nl‘: ‘23‘, ‘shg‘: ‘175‘}]> # [{‘xb‘: ‘女‘, ‘gz‘: ‘1500‘, ‘id‘: 4, ‘xm‘: ‘张三4‘, ‘nl‘: ‘21‘, ‘shg‘: ‘174‘}, {‘xb‘: ‘女‘, ‘gz‘: ‘1500‘, ‘id‘: 5, ‘xm‘: ‘张三5‘, ‘nl‘: ‘23‘, ‘shg‘: ‘175‘}]
由于json.dumps时无法处理datetime日期,所以可以通过自定义处理器来做扩展,如:
queryset对象 import json from datetime import date from datetime import datetime class JsonCustomEncoder(json.JSONEncoder): def default(self, field): if isinstance(field, datetime): return o.strftime(‘%Y-%m-%d %H:%M:%S‘) elif isinstance(field, date): return o.strftime(‘%Y-%m-%d‘) else: return json.JSONEncoder.default(self, field) # ds = json.dumps(queryset对象, cls=JsonCustomEncoder)
补充: models 中实现一个字段的默认值为当前登录用户
class ding_dan_guan_liAdmin(object): list_display = [‘nnd‘, ‘ke_hu_qing_kuang‘, ‘luyinyuan_chengben‘, ‘shou_kuan_qing_kuang‘, ‘ding_dan_zhuang_tai‘, ‘jiaoyi_qingkuang‘, ‘wengao_fabu‘] model_icon = ‘fa fa-pencil-square-o‘ readonly_fields = [‘nnd‘, ‘xiadan_ren‘] style_fields = {‘bei_zhu‘: ‘ueditor‘, ‘wen_gao‘: ‘ueditor‘} # 使用富文本框插件 def save_models(self): obj = self.new_obj request = self.request # 获取添加到订单用户值 yhu = obj.xiadan_ren if not yhu: obj.xiadan_ren = request.user obj.save() xadmin.site.register(ding_dan_guan_li, ding_dan_guan_liAdmin)