from app import models
#查询所有
doc = models.Users.objects.filter()
print(doc) # <QuerySet [<Users: Users object (1)>]>
# 条件查询-当返回为空时
# filter(**args)
doc = models.Users.objects.filter(email=body["email"])
print(doc) # <QuerySet []>
if not doc:
print('查询为空')
# filter(**args).values()
doc = models.Users.objects.filter(email=body["email"]).values()
print(doc) # <QuerySet []>
if not doc:
print('查询为空')
# filter(**args).first()
doc = models.Users.objects.filter(email=body["email"]).first()
print(doc) # None
if not doc:
print('查询为空')
# 条件查询-当返回不为空时
# filter(**args)
doc = models.Users.objects.filter(email=body["email"])
print(doc) # <QuerySet [<Users: Users object (1)>]>
data_dict = [] # 定义数组
for i in doc:
data_dict.append({ # 添加每一个对象
"id": i.id,
"username": i.username,
"email": i.email,
"password": i.password,
"status": i.status,
"role_title": i.role.title, # 去关联的角色表查询角色名称
"role_permissions_menu": i.role.permissions.menu # 查询权限
})
print(data_dict, 'data_dict')
# [{'id': 1, 'username': 'admin', 'email': '13400405244@163.com', 'password': 'admin', 'status': 1, 'role_title': '超级管理员', 'role_permissions_menu': '["all_btn","all_router"]'}] data_dict
# filter(**args).first()
doc = models.Users.objects.filter(email=body["email"]).first()
print(doc) # Users object (1)
print(doc.username) # admin
print(doc.role.title) # 超级管理员
# filter(**args).values() 需要list一下
doc = list(models.Users.objects.filter(email=body["email"]).values())
print(doc, "doc")
# [{'id': 1, 'username': 'admin', 'email': '13400405244@163.com', 'password': 'admin', 'status': 1, 'role_id': 1}] doc
res_ls = []
for i in doc:
role = models.Role.objects.get(id=i["role_id"])
print(role, "role") # Role object (1) role
res_ls.append({
"id": i["id"],
"username": i["username"],
"email": i["email"],
"password": i["password"],
"status": i["status"],
"role_title": role.title,
"role_permissions_menu": role.permissions.menu
})
print(res_ls, 'res_ls')
# [{'id': 1, 'username': 'admin', 'email': '13400405244@163.com', 'password': 'admin', 'status': 1, 'role_title': '超级管理员', 'role_permissions_menu': '["all_btn","all_router"]'}] res_ls
# filter(**args]).values().first()
doc = models.Users.objects.filter(email=body["email"]).values().first()
print(doc)
# {'id': 1, 'username': 'admin', 'email': '13400405244@163.com', 'password': 'admin', 'status': 1, 'role_id': 1}
role = models.Role.objects.get(id=doc["role_id"])
print(role, "role") # Role object (1) role
data_dict = {
"id": doc["id"],
"username": doc["username"],
"email": doc["email"],
"password": doc["password"],
"status": doc["status"],
"role_title": role.title,
"role_permissions_menu": role.permissions.menu
}
print(data_dict, 'data_dict')
# {'id': 1, 'username': 'admin', 'email': '13400405244@163.com', 'password': 'admin', 'status': 1, 'role_title': '超级管理员', 'role_permissions_menu': '["all_btn","all_router"]'} data_dict
# all()
doc = models.Users.objects.all()
print(doc) # <QuerySet [<Users: Users object (1)>]>
data_dict = [] # 定义数组
for i in doc:
data_dict.append({ # 添加每一个对象
"id": i.id,
"username": i.username,
"email": i.email,
"password": i.password,
"status": i.status,
"role_title": i.role.title, # 去关联的角色表查询角色名称
"role_permissions_menu": i.role.permissions.menu # 查询权限
})
print(data_dict, 'data_dict')
# [{'id': 1, 'username': 'admin', 'email': '13400405244@163.com', 'password': 'admin', 'status': 1, 'role_title': '超级管理员', 'role_permissions_menu': '["all_btn","all_router"]'}] data_dict