這篇文章主要介紹了Django怎么實(shí)現(xiàn)外鍵去除自動添加的后綴‘_id’,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
django在使用外鍵ForeignKey的時(shí)候,會自動給當(dāng)前字段后面添加一個后綴_id。
正常來說這樣并不會影響使用。除非你要寫原生sql,還有就是這個表是已經(jīng)存在的,你只是把數(shù)據(jù)庫中的表映射回models。實(shí)際上django提供了這樣的一個關(guān)鍵字db_colnum來解決這個問題,你只需要:
f = models.ForeignKey(AnotherModel, db_column='f')
這樣就不會自動添加_id這個后綴了。
文檔中是這么解釋的:
The name of the database column to use for this field. If this isn't given, Django will use the field's name.
If your database column name is an SQL reserved word, or contains characters that aren't allowed in Python variable names – notably, the hyphen – that's OK. Django quotes column and table names behind the scenes.
https://docs.djangoproject.com/en/dev/ref/models/fields/#db-column
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Django怎么實(shí)現(xiàn)外鍵去除自動添加的后綴‘_id’”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
當(dāng)前標(biāo)題:Django怎么實(shí)現(xiàn)外鍵去除自動添加的后綴‘_id’-創(chuàng)新互聯(lián)
文章分享:http://m.kartarina.com/article46/ccieeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、ChatGPT、自適應(yīng)網(wǎng)站、網(wǎng)站營銷、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容