python-django-自定义查询Q函数和F函数
数据库:
def page_q(request): """Q函数的使用""" #查询username和nickname都是zhangsan user_list = WeiboUser.objects.filter(username='zhangsan') user2_list = WeiboUser.objects.filter(nickname='zhangsan') print(user_list) print(user2_list) # OR | name = request.GET.get('name',None)#通过URL进行参数得传递,默认无 query = Q(username=name) | Q(nickname=name) user_listQ = WeiboUser.objects.filter(query) print(user_listQ) print('SSSSSSSSSSSSSSSSSSSSSSS') # AND & #查询名字和昵称是某某得用户 #username 按用户名查血 username = request.GET.get('username',None) # nickname 按昵称擦化学 nickname = request.GET.get('nickname',None) query = Q()#避免下面不进入if语句里面 if username is not None: query = query & Q(username=username) if nickname is not None: query = query & Q(nickname=nickname) user_list_q2 = WeiboUser.objects.filter(query) print(user_list_q2.count()) return HttpResponse('dddd')
URL: http://127.0.0.1:8000/weibo/Q/?nickname=zhangsan 结果为 [08/May/2020 22:32:41] "GET /weibo/Q/?username=zhangsan HTTP/1.1" 200 4 1 <QuerySet [<WeiboUser: User:zhangsan pk:3,nickname:zhangsan>, <WeiboUser: User:zhangsan pk:4,nickname:qq>]> <QuerySet [<WeiboUser: User:zhangsan pk:3,nickname:zhangsan>, <WeiboUser: User:duser1 pk:9,nickname:zhangsan>, <WeiboUser: User:duser2 pk:10,nickname:zhangsan>]> <QuerySet []> SSSSSSSSSSSSSSSSSSSSSSS 3 [08/M
URL http://127.0.0.1:8000/weibo/Q/?username=zhangsan 结果: [08/May/2020 22:32:57] "GET /weibo/Q/?nickname=zhangsan HTTP/1.1" 200 4 <QuerySet [<WeiboUser: User:zhangsan pk:3,nickname:zhangsan>, <WeiboUser: User:zhangsan pk:4,nickname:qq>]> <QuerySet [<WeiboUser: User:zhangsan pk:3,nickname:zhangsan>, <WeiboUser: User:duser1 pk:9,nickname:zhangsan>, <WeiboUser: User:duser2 pk:10,nickname:zhangsan>]> <QuerySet []> SSSSSSSSSSSSSSSSSSSSSSS 2
URL: http://127.0.0.1:8000/weibo/Q/?name=zhangsan 结果: [08/May/2020 22:38:22] "GET /weibo/Q/?username=zhangsan HTTP/1.1" 200 4 <QuerySet [<WeiboUser: User:zhangsan pk:3,nickname:zhangsan>, <WeiboUser: User:zhangsan pk:4,nickname:qq>]> <QuerySet [<WeiboUser: User:zhangsan pk:3,nickname:zhangsan>, <WeiboUser: User:duser1 pk:9,nickname:zhangsan>, <WeiboUser: User:duser2 pk:10,nickname:zhangsan>]> <QuerySet [<WeiboUser: User:zhangsan pk:3,nickname:zhangsan>, <WeiboUser: User:zhangsan pk:4,nickname:qq>, <WeiboUser: User:duser1 pk:9,nickname:zhangsan>, <WeiboUser: User:duser2 pk:10,nickname:zhangsan>]> SSSSSSSSSSSSSSSSSSSSSSS 106 由于这里没有传入username 和nickname所以查询为没条件,所以全部得数据都查询出来了
import threading from django.db.models import F from weibo.models import WeiboUser class ChangeThread(threading.Thread): """改变用户的状态""" def __init__(self,max_count=100,*args,**kwargs): super().__init__(*args,**kwargs) self.max_count = max_count def run(self): count = 0 user = WeiboUser.objects.get(pk=2) while True: if count >= self.max_count: break print(self.getName(),count) # user.status+=1 user.status = F('status') + 1 #F函数表示每次操作都要去数据库查询出数据进行炒作 user.save() count +=1 def main(): t1 = ChangeThread(max_count=800) t2 = ChangeThread(max_count=500) t1.start() t2.start() t1.join() t2.join()