What's the difference between Model.id and Mod

2020-02-01 06:55发布

问题:

I saw the django documents use both sometimes? Are they identical? What's the difference and where is the reference? I only see documentation of pk.

BTW, does django reference cover all the methods of its classes?

回答1:

pk is the attribute that contains the value of the primary key for the model. id is the name of the field created as a primary key by default if none is explicitly specified.



回答2:

pk is independent of the actual primary key. id is more useful when you have models with different primary key fields. It does not matter which one you use, however, it is recommend to use pk.