澶?/em>涓矇涓濄€?涓嶈繃锛屽湪瀛︾敓鍜岃€佸笀鐨勪緥瀛愪腑锛屽瀵瑰鍏崇郴鍏宠仈浜嗕袱涓疄浣撱€?浣嗗湪绮変笣鍏崇郴涓紝鐢ㄦ埛鍏虫敞鍏朵粬鐢ㄦ埛锛屽彧鏈変竴涓敤鎴峰疄浣撱€?閭d箞锛屽瀵瑰鍏崇郴鐨勭浜屼釜瀹炰綋鏄粈涔堝憿锛?/p>璇ュ叧绯荤殑绗簩涓疄浣撲篃鏄敤鎴枫€?涓€涓被鐨勫疄渚嬭鍏宠仈鍒板悓涓€涓被鐨勫叾浠栧疄渚嬬殑鍏崇郴琚О涓?em style="box-sizing: border-box;">鑷紩鐢ㄥ叧绯?/em>锛岃繖姝f槸鎴戝湪杩欓噷鎵€鐢ㄥ埌鐨勩€?/p>
浣跨敤鑷紩鐢ㄥ瀵瑰鍏崇郴鏉ュ疄鐜扮矇涓濇満鍒剁殑琛ㄧ粨鏋勭ず鎰忓浘锛?/p>
followers
琛ㄦ槸鍏崇郴鐨勫叧鑱旇〃銆?姝よ〃涓殑澶栭敭閮芥寚鍚戠敤鎴疯〃涓殑鏁版嵁琛岋紝鍥犱负瀹冨皢鐢ㄦ埛鍏宠仈鍒扮敤鎴枫€?璇ヨ〃涓殑姣忎釜璁板綍浠h〃鍏虫敞鑰呭拰琚叧娉ㄨ€呯殑涓€涓叧绯汇€?鍍忓鐢熷拰鑰佸笀鐨勪緥瀛愪竴鏍凤紝鍍忚繖鏍风殑璁捐鍏佽鏁版嵁搴撳洖绛旀墍鏈夊叧浜庡叧娉ㄥ拰琚叧娉ㄧ殑闂锛屽苟涓旇冻澶熷共鍑€鍒╄惤銆?/p>
鏁版嵁搴撴ā鍨嬬殑瀹炵幇
棣栧厛锛岃鎴戜滑鍦ㄦ暟鎹簱涓坊鍔犵矇涓濇満鍒跺惂銆傝繖鏄?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">followers鍏宠仈琛細
杩欐槸涓婂浘涓叧鑱旇〃鐨勭洿鎺ョ炕璇戙€?璇锋敞鎰忥紝鎴戞病鏈夊儚鎴戜负鐢ㄦ埛鍜岀敤鎴峰姩鎬佹墍鍋氱殑閭f牱锛屽皢琛ㄥ0鏄庝负妯″瀷銆?鍥犱负杩欐槸涓€涓櫎浜嗗閿病鏈夊叾浠栨暟鎹殑杈呭姪琛紝鎵€浠ユ垜鍒涘缓瀹冪殑鏃跺€欐病鏈夊叧鑱斿埌妯″瀷绫汇€?/p>
鐜板湪鎴戝彲浠ュ湪鐢ㄦ埛琛ㄤ腑澹版槑澶氬澶氱殑鍏崇郴浜嗭細
寤虹珛鍏崇郴鐨勮繃绋嬪疄灞炰笉鏄撱€?灏卞儚鎴戜负post
涓€瀵瑰鍏崇郴鎵€鍋氱殑閭f牱锛屾垜浣跨敤db.relationship
鍑芥暟鏉ュ畾涔夋ā鍨嬬被涓殑鍏崇郴銆?杩欑鍏崇郴灏?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">User瀹炰緥鍏宠仈鍒板叾浠?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">User瀹炰緥锛屾墍浠ユ寜鐓ф儻渚嬶紝瀵逛簬閫氳繃杩欑鍏崇郴鍏宠仈鐨勪竴瀵圭敤鎴锋潵璇达紝宸︿晶鐢ㄦ埛鍏虫敞鍙充晶鐢ㄦ埛銆?鎴戝湪宸︿晶鐨勭敤鎴蜂腑瀹氫箟浜?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">followed鐨勫叧绯伙紝鍥犱负褰撴垜浠庡乏渚ф煡璇㈣繖涓叧绯绘椂锛屾垜灏嗗緱鍒板凡鍏虫敞鐨勭敤鎴峰垪琛紙鍗冲彸渚х殑鍒楄〃锛夈€?璁╂垜浠€愪釜妫€鏌ヨ繖涓?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">db.relationship()鎵€鏈夌殑鍙傛暟锛?/p>
鈥楿ser鈥?/code>鏄叧绯诲綋涓殑鍙充晶瀹炰綋锛堝皢宸︿晶瀹炰綋鐪嬫垚鏄笂绾х被锛夈€傜敱浜庤繖鏄嚜寮曠敤鍏崇郴锛屾墍浠ユ垜涓嶅緱涓嶅湪涓や晶閮戒娇鐢ㄥ悓涓€涓疄浣撱€?/li>secondary
鎸囧畾浜嗙敤浜庤鍏崇郴鐨勫叧鑱旇〃锛屽氨鏄娇鐢ㄦ垜鍦ㄤ笂闈㈠畾涔夌殑followers
銆?/li>primaryjoin
鎸囨槑浜嗛€氳繃鍏崇郴琛ㄥ叧鑱斿埌宸︿晶瀹炰綋锛堝叧娉ㄨ€咃級鐨勬潯浠?銆傚叧绯讳腑鐨勫乏渚х殑join鏉′欢鏄叧绯昏〃涓殑follower_id
瀛楁涓庤繖涓叧娉ㄨ€呯殑鐢ㄦ埛ID鍖归厤銆?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">followers.c.follower_id
琛ㄨ揪寮忓紩鐢ㄤ簡璇ュ叧绯昏〃涓殑follower_id
鍒椼€?/li>secondaryjoin
鎸囨槑浜嗛€氳繃鍏崇郴琛ㄥ叧鑱斿埌鍙充晶瀹炰綋锛堣鍏虫敞鑰咃級鐨勬潯浠?銆?杩欎釜鏉′欢涓?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">primaryjoin绫讳技锛屽敮涓€鐨勫尯鍒湪浜庯紝鐜板湪鎴戜娇鐢ㄥ叧绯昏〃鐨勫瓧娈电殑鏄?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">followed_id浜嗐€?/li>backref
瀹氫箟浜嗗彸渚у疄浣撳浣曡闂鍏崇郴銆傚湪宸︿晶锛屽叧绯昏鍛藉悕涓?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">followed锛屾墍浠ュ湪鍙充晶鎴戝皢浣跨敤followers
鏉ヨ〃绀烘墍鏈夊乏渚х敤鎴风殑鍒楄〃锛屽嵆绮変笣鍒楄〃銆傞檮鍔犵殑lazy
鍙傛暟琛ㄧず杩欎釜鏌ヨ鐨勬墽琛屾ā寮忥紝璁剧疆涓哄姩鎬佹ā寮忕殑鏌ヨ涓嶄細绔嬪嵆鎵ц锛岀洿鍒拌璋冪敤锛岃繖涔熸槸鎴戣缃敤鎴峰姩鎬佷竴瀵瑰鐨勫叧绯荤殑鏂瑰紡銆?/li>lazy
鍜?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">backref涓殑lazy
绫讳技锛屽彧涓嶈繃褰撳墠鐨勮繖涓槸搴旂敤浜庡乏渚у疄浣擄紝backref
涓殑鏄簲鐢ㄤ簬鍙充晶瀹炰綋銆?/li>
濡傛灉鐞嗚В璧锋潵姣旇緝鍥伴毦锛屼綘涔熶笉蹇呰繃浜庢媴蹇冦€傛垜寰呬細鍎垮氨浼氬悜浣犲睍绀哄浣曞埄鐢ㄨ繖浜涘叧绯绘潵鎵ц鏌ヨ锛屼竴鍒囧氨浼氬彉寰楁竻鏅版槑浜嗐€?/p>
鏁版嵁搴撶殑鍙樻洿锛岄渶瑕佽褰曞埌涓€涓柊鐨勬暟鎹簱杩佺Щ涓細
鍏虫敞鍜屽彇娑堝叧娉?/h2>
鎰熻阿SQLAlchemy ORM锛屼竴涓敤鎴峰叧娉ㄥ彟涓€涓敤鎴风殑琛屼负鍙互閫氳繃followed
鍏崇郴鎶借薄鎴愪竴涓垪琛ㄦ潵绠€渚夸娇鐢ㄣ€?渚嬪锛屽鏋滄垜鏈変袱涓敤鎴峰瓨鍌ㄥ湪user1
鍜?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">user2鍙橀噺涓紝鎴戝彲浠ョ敤涓嬮潰杩欎釜绠€鍗曠殑璇彞鏉ュ疄鐜帮細
瑕佸彇娑堝叧娉ㄨ鐢ㄦ埛锛屾垜鍙互杩欎箞鍋氾細
鍗充究鍏虫敞鍜屽彇娑堝叧娉ㄧ殑鎿嶄綔鐩稿綋瀹规槗锛屾垜浠嶇劧鎯虫彁楂樿繖娈典唬鐮佺殑鍙噸鐢ㄦ€э紝鎵€浠ユ垜涓嶄細鐩存帴鍦ㄤ唬鐮佷腑浣跨敤鈥渁ppends鈥濆拰鈥渞emoves鈥濓紝鍙栬€屼唬涔嬶紝鎴戝皢鍦?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">User妯″瀷涓疄鐜扳€渇ollow鈥濆拰鈥渦nfollow鈥濇柟娉曘€?鏈€濂藉皢搴旂敤閫昏緫浠庤鍥惧嚱鏁拌浆绉诲埌妯″瀷鎴栧叾浠栬緟鍔╃被鎴栬緟鍔╂ā鍧椾腑锛屽洜涓轰綘浼氬湪鏈珷涔嬪悗灏嗕細鐪嬪埌锛岃繖浣垮緱鍗曞厓娴嬭瘯鏇村姞瀹规槗銆?/p>
涓嬮潰鏄敤鎴锋ā鍨嬩腑娣诲姞鍜屽垹闄ゅ叧娉ㄥ叧绯荤殑浠g爜鍙樻洿锛?/p>
follow()
鍜?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">unfollow()鏂规硶浣跨敤鍏崇郴瀵硅薄鐨?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">append()鍜?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">remove()鏂规硶銆傛湁蹇呰鍦ㄥ鐞嗗叧绯讳箣鍓嶏紝浣跨敤涓€涓?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">is_following()鏂规硶鏉ョ‘璁ゆ搷浣滅殑鍓嶆彁鏉′欢鏄惁绗﹀悎锛屼緥濡傦紝濡傛灉鎴戣姹?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">user1鍏虫敞user2
锛屼絾浜嬪疄璇佹槑杩欎釜鍏崇郴鍦ㄦ暟鎹簱涓凡缁忓瓨鍦紝鎴戝氨娌″繀瑕侀噸澶嶆搷浣滀簡銆?鐩稿悓鐨勯€昏緫鍙互搴旂敤浜庡彇娑堝叧娉ㄣ€?/p>
is_following()
鏂规硶鍙戝嚭涓€涓叧浜?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">followed鍏崇郴鐨勬煡璇㈡潵妫€鏌ヤ袱涓敤鎴蜂箣闂寸殑鍏崇郴鏄惁宸茬粡瀛樺湪銆?浣犲凡缁忕湅鍒拌繃鎴戜娇鐢⊿QLAlchemy鏌ヨ瀵硅薄鐨?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">filter_by()鏂规硶锛屼緥濡傦紝鏌ユ壘缁欏畾鐢ㄦ埛鍚嶇殑鐢ㄦ埛銆?鎴戝湪杩欓噷浣跨敤鐨?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">filter()鏂规硶寰堢被浼硷紝浣嗘槸鏇村姞鍋忓悜搴曞眰锛屽洜涓哄畠鍙互鍖呭惈浠绘剰鐨勮繃婊ゆ潯浠讹紝鑰屼笉鍍?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">filter_by()锛屽畠鍙兘妫€鏌ユ槸鍚︾瓑浜庝竴涓父閲忓€笺€?鎴戝湪is_following()
涓娇鐢ㄧ殑杩囨护鏉′欢鏄紝鏌ユ壘鍏宠仈琛ㄤ腑宸︿晶澶栭敭璁剧疆涓?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">self鐢ㄦ埛涓斿彸渚ц缃负user
鍙傛暟鐨勬暟鎹銆?鏌ヨ浠?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">count()鏂规硶缁撴潫锛岃繑鍥炵粨鏋滅殑鏁伴噺銆?杩欎釜鏌ヨ鐨勭粨鏋滄槸0
鎴?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">1锛屽洜姝ゆ鏌ヨ鏁版槸1杩樻槸澶т簬0瀹為檯涓婃槸鐩哥瓑鐨勩€?鑷充簬鍏朵粬鐨勬煡璇㈢粨鏉熺all()
鍜?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">first()锛屼綘宸茬粡鐪嬪埌鎴戜娇鐢ㄨ繃浜嗐€?/p>
鏌ョ湅宸插叧娉ㄧ敤鎴风殑鍔ㄦ€?/h2>
鍦ㄦ暟鎹簱涓敮鎸佺矇涓濇満鍒剁殑宸ヤ綔鍑犺繎灏惧0锛屼絾鏄垜鍗撮仐婕忎簡涓€椤归噸瑕佺殑鍔熻兘銆傚簲鐢ㄤ富椤典腑闇€瑕佸睍绀哄凡鐧诲綍鐢ㄦ埛鍏虫敞鐨勫叾浠栨墍鏈夌敤鎴风殑鍔ㄦ€侊紝鎴戦渶瑕佺敤鏁版嵁搴撴煡璇㈡潵杩斿洖杩欎簺鐢ㄦ埛鍔ㄦ€併€?/p>
鏈€鏄捐€屾槗瑙佺殑鏂规鏄厛鎵ц涓€涓煡璇互杩斿洖宸插叧娉ㄧ敤鎴风殑鍒楄〃锛屽浣犳墍鐭ワ紝鍙互浣跨敤user.followed.all()
璇彞銆傜劧鍚庡姣忎釜宸插叧娉ㄧ殑鐢ㄦ埛鎵ц涓€涓煡璇㈡潵杩斿洖浠栦滑鐨勭敤鎴峰姩鎬併€傛渶鍚庡皢鎵€鏈夌敤鎴风殑鍔ㄦ€佹寜鐓ф棩鏈熸椂闂村€掑簭鍚堝苟鍒颁竴涓垪琛ㄤ腑銆傚惉璧锋潵涓嶉敊锛熷叾瀹炰笉鐒躲€?/p>
杩欑鏂规硶鏈夊嚑涓棶棰樸€?濡傛灉涓€涓敤鎴峰叧娉ㄤ簡涓€鍗冧汉锛屼細鍙戠敓浠€涔堬紵 鎴戦渶瑕佹墽琛屼竴鍗冧釜鏁版嵁搴撴煡璇㈡潵鏀堕泦鎵€鏈夌殑鐢ㄦ埛鍔ㄦ€併€?鐒跺悗鎴戦渶瑕佸悎骞跺拰鎺掑簭鍐呭瓨涓殑涓€鍗冧釜鍒楄〃銆?浣滀负绗簩涓棶棰橈紝鑰冭檻鍒板簲鐢ㄤ富椤垫渶缁堝皢瀹炵幇鍒嗛〉锛屾墍浠ュ畠涓嶄細鏄剧ず鎵€鏈夊彲鐢ㄧ殑鐢ㄦ埛鍔ㄦ€侊紝鍙兘鏄墠鍑犱釜锛屽苟鏄剧ず涓€涓摼鎺ユ潵鎻愪緵鎰熷叴瓒g殑鐢ㄦ埛鏌ョ湅鏇村鍔ㄦ€併€?濡傛灉鎴戣鎸夊畠浠殑鏃ユ湡鎺掑簭鏉ユ樉绀哄姩鎬侊紝鎴戞€庝箞鑳界煡閬撳摢浜涚敤鎴峰姩鎬佹墠鏄墍鏈夌敤鎴蜂腑鏈€鏂扮殑鍛紵闄ら潪鎴戦鍏堝緱鍒颁簡鎵€鏈夌殑鐢ㄦ埛鍔ㄦ€佸苟瀵瑰叾杩涜鎺掑簭銆?杩欏疄闄呬笂鏄竴涓碂绯曠殑瑙e喅鏂规锛屼笉鑳藉緢濂藉湴搴斿瑙勬ā鍖栥€?/p>
鐢ㄦ埛鍔ㄦ€佺殑鍚堝苟鍜屾帓搴忔搷浣滄槸鏃犳硶閬垮厤鐨勶紝浣嗘槸鍦ㄥ簲鐢ㄤ腑鎵ц浼氬鑷存晥鐜囧崄鍒嗕綆涓嬶紝 鑰岃繖绉嶅伐浣滄槸鍏崇郴鏁版嵁搴撴搮闀跨殑銆?鎴戝彲浠ヤ娇鐢ㄦ暟鎹簱鐨勭储寮曪紝鍛戒护瀹冧互鏇存湁鏁堢殑鏂瑰紡鎵ц鏌ヨ鍜屾帓搴忋€?鎵€浠ユ垜鐪熸鎯宠鎻愪緵鐨勬柟妗堟槸锛屽畾涔夋垜鎯宠寰楀埌鐨勪俊鎭潵鎵ц涓€涓暟鎹簱鏌ヨ锛岀劧鍚庤鏁版嵁搴撴壘鍑哄浣曚互鏈€鏈夋晥鐨勬柟寮忔潵鎻愬彇杩欎簺淇℃伅銆?/p>
鐪嬬湅涓嬮潰鐨勮繖涓煡璇細
杩欐槸杩勪粖涓烘鎴戝湪杩欎釜搴旂敤涓娇鐢ㄧ殑鏈€澶嶆潅鐨勬煡璇€?鎴戝皢灏濊瘯涓€姝ヤ竴姝ュ湴瑙h杩欎釜鏌ヨ銆?濡傛灉浣犵湅涓€涓嬭繖涓煡璇㈢殑缁撴瀯锛屼綘浼氭敞鎰忓埌鏈変笁涓富瑕侀儴鍒嗭紝鍒嗗埆鏄?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">join()銆?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">filter()鍜?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">order_by()锛屼粬浠兘鏄疭QLAlchemy鏌ヨ瀵硅薄鐨勬柟娉曪細
鑱斿悎鏌ヨ
瑕佺悊瑙oin鎿嶄綔鐨勫姛鑳斤紝鎴戜滑鏉ョ湅涓€涓緥瀛愩€?鍋囪鎴戞湁涓€涓寘鍚互涓嬪唴瀹圭殑User
琛細
id | username |
---|
1 | john |
2 | susan |
3 | mary |
4 | david |
涓轰簡绠€鍗曡捣瑙侊紝鎴戝彧浼氫繚鐣欑敤鎴锋ā鍨嬬殑id
鍜?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">username瀛楁浠ヤ究杩涜鏌ヨ锛屽叾浠栫殑閮界暐鍘汇€?/p>
鍋囪followers
鍏崇郴琛ㄤ腑鏁版嵁琛ㄨ揪鐨勬槸鐢ㄦ埛john
鍏虫敞鐢ㄦ埛susan
鍜?nbsp;david
锛岀敤鎴?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">susan鍏虫敞鐢ㄦ埛mary
锛岀敤鎴?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">mary鍏虫敞鐢ㄦ埛david
銆傝繖浜涚殑鏁版嵁濡備笅琛ㄦ墍绀猴細
follower_id | followed_id |
---|
1 | 2 |
1 | 4 |
2 | 3 |
3 | 4 |
鏈€鍚庯紝鐢ㄦ埛鍔ㄦ€佽〃涓寘鍚簡姣忎釜鐢ㄦ埛鐨勪竴鏉″姩鎬侊細
id | text | user_id |
---|
1 | post from susan | 2 |
2 | post from mary | 3 |
3 | post from david | 4 |
4 | post from john | 1 |
杩欏紶琛ㄤ篃鐪佺暐浜嗕竴浜涗笉灞炰簬杩欎釜璁ㄨ鑼冨洿鐨勫瓧娈点€?/p>
杩欐槸鎴戜负璇ユ煡璇㈠啀娆¤璁$殑join()
璋冪敤锛?/p>
鎴戝湪鐢ㄦ埛鍔ㄦ€佽〃涓婅皟鐢╦oin鎿嶄綔銆?绗竴涓弬鏁版槸followers鍏宠仈琛紝绗簩涓弬鏁版槸join鏉′欢銆?鎴戠殑杩欎釜璋冪敤琛ㄨ揪鐨勫惈涔夋槸鎴戝笇鏈涙暟鎹簱鍒涘缓涓€涓复鏃惰〃锛屽畠灏嗙敤鎴峰姩鎬佽〃鍜屽叧娉ㄨ€呰〃涓殑鏁版嵁缁撳悎鍦ㄤ竴璧枫€?鏁版嵁灏嗘牴鎹弬鏁颁紶閫掔殑鏉′欢杩涜鍚堝苟銆?/p>
鎴戜娇鐢ㄧ殑鏉′欢琛ㄧず浜唂ollowers鍏崇郴琛ㄧ殑followed_id
瀛楁蹇呴』绛変簬鐢ㄦ埛鍔ㄦ€佽〃鐨?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">user_id瀛楁銆?瑕佹墽琛屾鍚堝苟锛屾暟鎹簱灏嗕粠鐢ㄦ埛鍔ㄦ€佽〃锛坖oin鐨勫乏渚э級鑾峰彇姣忔潯璁板綍锛屽苟杩藉姞followers
鍏崇郴琛紙join鐨勫彸渚э級涓殑鍖归厤鏉′欢鐨勬墍鏈夎褰曘€?濡傛灉followers
鍏崇郴琛ㄤ腑鏈夊涓褰曠鍚堟潯浠讹紝閭d箞鐢ㄦ埛鍔ㄦ€佹暟鎹灏嗛噸澶嶅嚭鐜般€?濡傛灉瀵逛簬涓€涓粰瀹氱殑鐢ㄦ埛鍔ㄦ€侊紝followers鍏崇郴琛ㄤ腑鍗存病鏈夊尮閰嶏紝閭d箞璇ョ敤鎴峰姩鎬佺殑璁板綍涓嶄細鍑虹幇鍦╦oin鎿嶄綔鐨勭粨鏋滀腑銆?/p>
鍒╃敤鎴戜笂闈㈠畾涔夌殑绀轰緥鏁版嵁锛屾墽琛宩oin鎿嶄綔鐨勭粨鏋滃涓嬶細
id | text | user_id | follower_id | followed_id |
---|
1 | post from susan | 2 | 1 | 2 |
2 | post from mary | 3 | 2 | 3 |
3 | post from david | 4 | 1 | 4 |
3 | post from david | 4 | 3 | 4 |
娉ㄦ剰user_id
鍜?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">followed_id鍒楀湪鎵€鏈夋暟鎹涓兘鏄浉绛夌殑锛屽洜涓鸿繖鏄痡oin鏉′欢銆?鏉ヨ嚜鐢ㄦ埛john
鐨勭敤鎴峰姩鎬佷笉浼氬嚭鐜板湪涓存椂琛ㄤ腑锛屽洜涓鸿鍏虫敞鍒楄〃涓病鏈夊寘鍚?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">john鐢ㄦ埛锛屾崲鍙ヨ瘽璇达紝娌℃湁浠讳綍浜哄叧娉╦ohn銆?鑰屾潵鑷?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">david鐨勭敤鎴峰姩鎬佸嚭鐜颁簡涓ゆ锛屽洜涓鸿鐢ㄦ埛鏈変袱涓矇涓濄€?/p>
铏界劧鍒涘缓浜嗚繖涓猨oin鎿嶄綔锛屼絾鍗存病鏈夊緱鍒版兂瑕佺殑缁撴灉銆傝缁х画鐪嬩笅鍘伙紝鍥犱负杩欏彧鏄洿澶х殑鏌ヨ鐨勪竴閮ㄥ垎銆?/p>
杩囨护
Join鎿嶄綔缁欎簡鎴戜竴涓墍鏈夎鍏虫敞鐢ㄦ埛鐨勭敤鎴峰姩鎬佺殑鍒楄〃锛岃繙瓒呭嚭鎴戞兂瑕佺殑閭i儴鍒嗘暟鎹€?鎴戝彧瀵硅繖涓垪琛ㄧ殑涓€涓瓙闆嗘劅鍏磋叮鈥斺€旀煇涓敤鎴峰叧娉ㄧ殑鐢ㄦ埛浠殑鍔ㄦ€侊紝鎵€浠ユ垜闇€瑕佺敤filter()
鏉ュ墧闄ゆ墍鏈夋垜涓嶉渶瑕佺殑鏁版嵁銆?/p>
杩欐槸杩囨护閮ㄥ垎鐨勬煡璇㈣鍙ワ細
璇ユ煡璇㈡槸User
绫荤殑涓€涓柟娉曪紝self.id
琛ㄨ揪寮忔槸鎸囨垜鎰熷叴瓒g殑鐢ㄦ埛鐨処D銆?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">filter()鎸戦€変复鏃惰〃涓?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">follower_id鍒楃瓑浜庤繖涓狪D鐨勮锛屾崲鍙ヨ瘽璇达紝鎴戝彧淇濈暀follower(绮変笣)鏄鐢ㄦ埛鐨勬暟鎹€?/p>
鍋囧鎴戠幇鍦ㄥid
涓猴紤鐨勭敤鎴?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">john鑳界湅鍒扮殑鐢ㄦ埛鍔ㄦ€佹劅鍏磋叮锛岃繖鏄粠涓存椂琛ㄨ繃婊ゅ悗鐨勭粨鏋滐細
id | text | user_id | follower_id | followed_id |
---|
1 | post from susan | 2 | 1 | 2 |
3 | post from david | 4 | 1 | 4 |
杩欐鏄垜鎯宠鐨勭粨鏋滐紒
璇疯浣忥紝鏌ヨ鏄粠Post
绫讳腑鍙戝嚭鐨勶紝鎵€浠ュ敖绠℃垜鏇剧粡寰楀埌浜嗙敱鏁版嵁搴撳垱寤虹殑涓€涓复鏃惰〃鏉ヤ綔涓烘煡璇㈢殑涓€閮ㄥ垎锛屼絾缁撴灉灏嗘槸鍖呭惈鍦ㄦ涓存椂琛ㄤ腑鐨勭敤鎴峰姩鎬侊紝 鑰屼笉浼氬瓨鍦ㄧ敱浜庢墽琛宩oin鎿嶄綔娣诲姞鐨勫叾浠栧垪銆?/p>
鎺掑簭
鏌ヨ娴佺▼鐨勬渶鍚庝竴姝ユ槸瀵圭粨鏋滆繘琛屾帓搴忋€傝繖閮ㄥ垎鐨勬煡璇㈣鍙ュ涓嬶細
鍦ㄨ繖閲岋紝鎴戣璇寸殑鏄紝鎴戝笇鏈涗娇鐢ㄧ敤鎴峰姩鎬佷骇鐢熺殑鏃堕棿鎴虫寜闄嶅簭鎺掑垪缁撴灉鍒楄〃銆傛帓搴忎箣鍚庯紝绗竴涓粨鏋滃皢鏄渶鏂扮殑鐢ㄦ埛鍔ㄦ€併€?/p>
缁勫悎鑷韩鍔ㄦ€佸拰鍏虫敞鐨勭敤鎴峰姩鎬?/h2>
鎴戝湪followed_posts()
鍑芥暟涓娇鐢ㄧ殑鏌ヨ鏄潪甯告湁鐢ㄧ殑锛屼絾鏈変竴涓檺鍒讹紝浜轰滑鏈熸湜鐪嬪埌浠栦滑鑷繁鐨勫姩鎬佸寘鍚湪浠栦滑鐨勫叧娉ㄧ殑鐢ㄦ埛鍔ㄦ€佺殑鏃堕棿绾夸腑锛岃€岃鏌ヨ鍗村姏鏈夋湭閫€?/p>
鏈変袱绉嶅彲鑳界殑鏂瑰紡鏉ユ墿灞曟鏌ヨ浠ュ寘鍚敤鎴疯嚜宸辩殑鍔ㄦ€併€?鏈€鐩存埅浜嗗綋鐨勬柟娉曟槸灏嗘煡璇繚鎸佸師鏍凤紝浣嗚纭繚鎵€鏈夌敤鎴烽兘鍏虫敞浜嗕粬浠嚜宸便€?濡傛灉浣犳槸浣犺嚜宸辩殑绮変笣锛岄偅涔堜笂闈㈢殑鏌ヨ灏变細鎵惧埌浣犺嚜宸辩殑鍔ㄦ€佷互鍙婁綘鍏虫敞鐨勬墍鏈変汉鐨勫姩鎬併€?杩欑鏂规硶鐨勭己鐐规槸浼氬奖鍝嶇矇涓濈殑缁熻鏁版嵁銆?鎵€鏈変汉鐨勭矇涓濇暟閲忛兘灏嗗姞涓€锛屾墍浠ュ畠浠繀椤诲湪鏄剧ず涔嬪墠杩涜璋冩暣銆?绗簩绉嶆柟娉曟槸閫氳繃鍒涘缓绗簩涓煡璇㈣繑鍥炵敤鎴疯嚜宸辩殑鍔ㄦ€侊紝鐒跺悗浣跨敤鈥渦nion鈥濇搷浣滃皢涓や釜鏌ヨ鍚堝苟涓轰竴涓煡璇€?/p>
娣辨€濈啛铏戜箣鍚庯紝鎴戦€夋嫨浜嗙浜屼釜鏂规銆?涓嬮潰浣犲彲浠ョ湅鍒?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">followed_posts()鍑芥暟宸茶鎵╁睍鎴愰€氳繃鑱斿悎鏌ヨ鏉ュ苟鍏ョ敤鎴疯嚜宸辩殑鍔ㄦ€侊細
璇锋敞鎰忥紝followed
鍜?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">own鏌ヨ缁撴灉闆嗘槸鍦ㄦ帓搴忎箣鍓嶈繘琛岀殑鍚堝苟銆?/p>
瀵圭敤鎴锋ā鍨嬫墽琛屽崟鍏冩祴璇?/h2>
铏界劧鎴戜笉鎷呭績杩欎釜绋嶆樉鈥滃鏉傗€濈殑绮変笣鏈哄埗鐨勮繍琛屾槸鍚︽棤璇€?浣嗗綋鎴戠紪鍐欎妇瓒宠交閲嶇殑浠g爜鏃讹紝鎴戞媴蹇冪殑鏄垜鍦ㄥ簲鐢ㄧ殑涓嶅悓閮ㄥ垎淇敼浜嗕唬鐮佷箣鍚庯紝濡備綍纭繚鏈浠g爜灏嗘潵浼氱户缁伐浣溿€?纭繚宸茬粡缂栧啓鐨勪唬鐮佸湪灏嗘潵缁х画鏈夋晥鐨勬渶浣虫柟娉曟槸鍒涘缓涓€濂楄嚜鍔ㄥ寲娴嬭瘯锛屼綘鍙互鍦ㄦ瘡娆℃洿鏂颁唬鐮佸悗鎵ц娴嬭瘯銆?/p>
Python鍖呭惈涓€涓潪甯告湁鐢ㄧ殑unittest
鍖咃紝鍙互杞绘澗缂栧啓鍜屾墽琛屽崟鍏冩祴璇曘€?璁╂垜浠潵涓?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">User绫讳腑鐨勭幇鏈夋柟娉曠紪鍐欎竴浜涘崟鍏冩祴璇曞苟瀛樺偍鍒?em style="box-sizing: border-box;">tests.py
妯″潡锛?/p>浠庣幇鍦ㄨ捣锛屾瘡娆″搴旂敤杩涜鏇存敼鏃讹紝閮藉彲浠ラ噸鏂拌繍琛屾祴璇曪紝浠ョ‘淇濇鍦ㄦ祴璇曠殑鍔熻兘娌℃湁鍙楀埌褰卞搷銆?鍙﹀锛屾瘡娆″皢鍙︿竴涓姛鑳芥坊鍔犲埌搴旂敤鏃讹紝閮藉簲璇ヤ负鍏剁紪鍐欎竴涓崟鍏冩祴璇曘€?/p>
鏁版嵁搴撳拰妯″瀷涓矇涓濇満鍒剁殑瀹炵幇鐜板湪宸茬粡瀹屾垚锛屼絾鏄垜娌℃湁灏嗗畠闆嗘垚鍒板簲鐢ㄤ腑锛屾墍浠ユ垜鐜板湪瑕佹坊鍔犺繖涓姛鑳姐€?鍊煎緱楂樺叴鐨勬槸锛屽疄鐜板畠娌℃湁浠€涔堝ぇ鐨勬寫鎴橈紝閮藉皢鍩轰簬浣犲凡缁忓杩囩殑姒傚康銆?/p>
瑙嗗浘鍑芥暟鐨勯€昏緫涓嶈█鑰屽柣锛屼絾瑕佹敞鎰忔墍鏈夌殑閿欒妫€鏌ワ紝浠ラ槻姝㈠嚭鐜版剰澶栫殑闂锛屽苟灏濊瘯鍦ㄥ嚭鐜伴棶棰樻椂鍚戠敤鎴锋彁渚涙湁鐢ㄧ殑淇℃伅銆?/p>
鐢ㄦ埛涓汉涓婚〉鐨勫彉鏇达紝涓€鏄湪鏈€杩戣闂殑鏃堕棿鎴充箣涓嬫坊鍔犱竴琛岋紝浠ユ樉绀烘鐢ㄦ埛鎷ユ湁澶氬皯涓矇涓濆拰鍏虫敞鐢ㄦ埛銆備簩鏄綋浣犳煡鐪嬭嚜宸辩殑涓汉涓婚〉鏃跺嚭鐜扮殑鈥淓dit鈥濋摼鎺ョ殑琛岋紝鍙兘浼氬彉鎴愪互涓嬩笁绉嶉摼鎺ヤ箣涓€锛?/p>
姝ゆ椂锛屼綘鍙互杩愯璇ュ簲鐢紝鍒涘缓涓€浜涚敤鎴峰苟娴嬭瘯涓€涓嬪叧娉ㄥ拰鍙栨秷鍏虫敞鐢ㄦ埛鐨勫姛鑳姐€?鍞竴闇€瑕佽浣忕殑鏄紝闇€瑕佹墜鍔ㄩ敭鍏ヤ綘瑕佸叧娉ㄦ垨鍙栨秷鍏虫敞鐨勭敤鎴风殑涓汉涓婚〉URL锛屽洜涓虹洰鍓嶆病鏈夊姙娉曟煡鐪嬬敤鎴峰垪琛ㄣ€?渚嬪锛屽鏋滀綘鎯冲叧娉?code style="box-sizing: border-box; text-align: left; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 2px 4px 0px; font-size: 0.9rem; background-color: rgb(248, 248, 248);">susan锛屽垯闇€瑕佸湪娴忚鍣ㄧ殑鍦板潃鏍忎腑杈撳叆http://localhost:5000/user/susan浠ヨ闂鐢ㄦ埛鐨勪釜浜轰富椤点€?璇风‘淇濅綘鍦ㄦ祴璇曞叧娉ㄥ拰鍙栨秷鍏虫敞鐨勬椂鍊欙紝鐣欐剰鍒颁簡鍏剁矇涓濆拰鍏虫敞鐨勬暟閲忓彉鍖栥€?/p>
鎴戝簲璇ュ湪搴旂敤鐨勪富椤典笂鏄剧ず鐢ㄦ埛鍔ㄦ€佺殑鍒楄〃锛屼絾鏄垜杩樻病鏈夊畬鎴愭墍鏈変緷璧栫殑宸ヤ綔锛屽洜涓虹敤鎴蜂笉鑳藉彂琛ㄥ姩鎬併€?鎵€浠ユ垜浼氭殏缂撹繖涓〉闈㈢殑瀹屽杽宸ヤ綔锛岀洿鍒板彂琛ㄧ敤鎴峰姩鎬佸姛鑳界殑瀹屾垚銆?/p>
!--EndFragment-->