用django作的第一個小項目django
作一個項目首先要把表結構設計好 既表與表之間的關係spa
models設計
一對一表查詢複習先code
#沒有過濾條件 默認取所有 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)