The django foreign key attribute name is different from the name in the database.

the django ORM model is as follows

class DeviceType(models.Model):
    name = models.CharField(_(u""), max_length=64)
    
class Device(models.Model)
    d_type = models.ForeignKey(DeviceType, db_column=u"device_type_id")
    -sharp mo"ren
    -sharp device_type = models.ForeignKey(DeviceType, db_column=u"device_type_id")
 

the usual way to name a foreign key is to add id to the default field name of Djano. Once specified using db_column, it is not accessible through the Device instance o.device_type_id, but through o.d_type_id. Does the naming of the django ORM foreign key field have nothing to do with db_column?

Mar.28,2021

nothing. The
field has two names, one is droomtypequalified, which is at the python level, and the other is device_type_id, which is the column name that ends up in the database. If you access Device through an instance, it is at the python level. If you visit device_type_id, you will get an error:

>>> d.device_type_id
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Device' object has no attribute 'device_type_id'

if you look at dir (Device) or dir (d) in python shell, you can't find device_type_id , only 'dbirthday typeexamples,' dskills typewriters, :

.
>>> d.d_type  -sharp  DeviceType 
<DeviceType: first type>
>>> d.d_type_id  -sharp device_type_id 
1
Menu