Django 實現外鍵去除自動添加的后綴‘_id’
django在使用外鍵ForeignKey的時候,會自動給當前字段后面添加一個后綴_id。
正常來說這樣并不會影響使用。除非你要寫原生sql,還有就是這個表是已經存在的,你只是把數據庫中的表映射回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 實現外鍵去除自動添加的后綴‘_id'就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
torchxrayvision包安裝過程(附pytorch1.6cpu版安裝)
這篇文章主要介紹了torchxrayvision包安裝過程(附pytorch1.6cpu版安裝),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
python 類對象和實例對象動態(tài)添加方法(分享)
下面小編就為大家分享一篇python 類對象和實例對象動態(tài)添加方法。具有很的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12

