Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing django from appending "_id" to a foreign key field

Tags:

python

django

In django, if I set a field in a model to a foreign key, "_id" is appended to the name of that field. How can this be prevented?

like image 898
niravb Avatar asked Nov 22 '11 07:11

niravb


People also ask

What is _ID in Django?

You'll always deal with the field names of your model object. So you could say that Django will construct a "twin" column, with an _id suffix. This column has the same type as the type of the primary key of the model you target, and that column will thus contain the primary key of the model object you use.

Does Django automatically index foreign keys?

Django automatically creates an index for all models. ForeignKey columns. From Django documentation: A database index is automatically created on the ForeignKey .

Can ForeignKey field be null Django?

django rest framework - ForeignKey does not allow null values - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.


1 Answers

You can set the field's db_column attribute to whatever you'd like.

like image 143
Sam Dolan Avatar answered Oct 03 '22 09:10

Sam Dolan