Django 實現(xiàn)外鍵去除自動添加的后綴‘_id’
django在使用外鍵ForeignKey的時候,會自動給當前字段后面添加一個后綴_id。
正常來說這樣并不會影響使用。除非你要寫原生sql,還有就是這個表是已經(jīng)存在的,你只是把數(shù)據(jù)庫中的表映射回models。實際上django提供了這樣的一個關鍵字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
以上這篇Django 實現(xiàn)外鍵去除自動添加的后綴‘_id'就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python實現(xiàn)word/excel/ppt批量轉(zhuǎn)pdf的示例代碼
這篇文章主要為大家詳細介紹了如何利用python實現(xiàn)word、excel、ppt批量轉(zhuǎn)pdf文件,文中的示例代碼講解詳細,有需要的小伙伴可以參考下2023-09-09Python數(shù)據(jù)分析之pandas比較操作
比較操作是很簡單的基礎知識,不過Pandas中的比較操作有一些特殊的點,本文介紹的非常詳細,對正在學習python的小伙伴們很有幫助.需要的朋友可以參考下2021-05-05安裝pycurl報錯Could not run curl-config: &ap
這篇文章主要為大家介紹了安裝pycurl報錯Could not run curl-config: 'curl-config'解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12python攻防-破解附近局域網(wǎng)WIFI密碼實現(xiàn)上網(wǎng)自由
本文將記錄學習如何通過 Python 腳本實破解附近局域網(wǎng) WIFI 密碼的暴力破解,隨時隨地免費蹭網(wǎng),再也不被WiFi密碼困擾,實現(xiàn)蹭網(wǎng)自由2021-08-08python 如何把docker-compose.yaml導入到數(shù)據(jù)庫相關條目里
這篇文章主要介紹了python 如何把docker-compose.yaml導入到數(shù)據(jù)庫相關條目里?下面小編就為大家介紹一下實現(xiàn)方式,一起跟隨小編過來看看吧2021-01-01