用django做的第一个小项目
做一个项目首先要把表结构设计好 既表与表之间的关系
models
一对一表查询复习先
#没有过滤条件 默认取全部 rez = models.UserInfo.objects.filter() #取到userinfo里第一个对象 ret = models.UserInfo.objects.filter().first() #filter是过滤条件得到符合条件的QuerSet对象列表,没写条件默认全取 #values是取哪个字段的 values返回的是一个特殊的QuerSet对象以字典的形式 rev = models.UserInfo.objects.filter().values() res = models.UserInfo.objects.filter().values().first() print(rez)#<QuerySet [<UserInfo: zhangxiangyu>, <UserInfo: shuaige>, <UserInfo: fengge>, <UserInfo: laji>]> print(ret)#zhangxiangyu print(rev)#<QuerySet [{'password': 'pbkdf2_sha256$36000$zp8ZbTg592rM$DnHyzSA7e01uPgT5w2BhxElnXeB+JCcDyxc8kIKqV1Q=', # 'last_login': datetime.datetime(2018, 7, 6, 14, 54, 45, 795147), # 'is_superuser': True, # 'username': 'zhangxiangyu', # 'first_name': '', # 'last_name': '', # 'email': '', # 'is_staff': True, # 'is_active': True, # 'date_joined': datetime.datetime(2018, 7, 2, 12, 20), # 'nid': 1, # 'phone': '1323456789', # 'avatar': 'avatars/lan.jpg', # 'blog_id': 1}, # {'password': 'pbkdf2_sha256$36000$1XDan2kCOZTM$YuGErlHmajPaP0Y6yekfE2DoAQNM0DT2iKGferJOK8o=', # 'last_login': datetime.datetime(2018, 7, 6, 20, 12, 52, 687070), # 'is_superuser': False, # 'username': 'shuaige', # 'first_name': '', # 'last_name': '', # 'email': '', # 'is_staff': False, # 'is_active': True, # 'date_joined': datetime.datetime(2018, 7, 3, 11, 14, 58, 682584), # 'nid': 2, # 'phone': '13564860073', # 'avatar': 'avatars/20170927154455_d935444a70089b866b462615580c1449_9.jpg', # 'blog_id': None}, # {'password': 'pbkdf2_sha256$36000$JxHqJPfwM5Br$86fF57EivL4jYSxUpoZq7Vms47f/O6qjL2WR/ljHvAo=', # 'last_login': None, # 'is_superuser': False, # 'username': 'fengge', # 'first_name': '', # 'last_name': '', # 'email': '', # 'is_staff': False, # 'is_active': True, # 'date_joined': datetime.datetime(2018, 7, 3, 11, 23, 5, 372292), # 'nid': 4, # 'phone': '13564860074', # 'avatar': 'avatars/64006a41a84a1a7271f938_f97B9Oz.png', # 'blog_id': None}, # {'password': 'pbkdf2_sha256$36000$45DjoTLDNOEb$fnlYjkGK3NGuj75YVHVYUAu22Xk1DUmlBOYTvS0R+v4=', # 'last_login': datetime.datetime(2018, 7, 4, 8, 59, 18, 729869), # 'is_superuser': False, # 'username': 'laji', # 'first_name': '', # 'last_name': '', # 'email': '', # 'is_staff': False, # 'is_active': True, # 'date_joined': datetime.datetime(2018, 7, 4, 8, 16, 41, 997415), # 'nid': 5, # 'phone': '13345678910', # 'avatar': 'avatars/18309411_1200x1000_0.jpg', # 'blog_id': None}]> print(rev[0]['password'])#pbkdf2_sha256$36000$zp8ZbTg592rM$DnHyzSA7e01uPgT5w2BhxElnXeB+JCcDyxc8kIKqV1Q= print(res)#{'password': 'pbkdf2_sha256$36000$zp8ZbTg592rM$DnHyzSA7e01uPgT5w2BhxElnXeB+JCcDyxc8kIKqV1Q=', # 'last_login': datetime.datetime(2018, 7, 6, 14, 54, 45, 795147), # 'is_superuser': True, # 'username': 'zhangxiangyu', # 'first_name': '', # 'last_name': '', # 'email': '', # 'is_staff': True, # 'is_active': True, # 'date_joined': datetime.datetime(2018, 7, 2, 12, 20), # 'nid': 1, # 'phone': '1323456789', # 'avatar': 'avatars/lan.jpg', # 'blog_id': 1} print(res['password'])#pbkdf2_sha256$36000$zp8ZbTg592rM$DnHyzSA7e01uPgT5w2BhxElnXeB+JCcDyxc8kIKqV1Q=
#得到一个userinfo表对象
res = models.UserInfo.objects.first()
print(res)#zhangxiangyu(这相当于一个对象)
#一对一关联的表可以将关联表名当做字段来查询但是它不等于某一个具体的值,而是等于一个对象
obj = models.Blog.objects.filter(userinfo=res)
print(obj[0].title)#zhangxiangyu的博客
#一对一跨表查询 可以用__加关联表的字段来进行查询
obj1 = models.Blog.objects.filter(userinfo__phone='13564860073').values('title')
print(obj1)