Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Model.id and Model.pk in django?

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?

like image 976
dspjm Avatar asked Mar 12 '14 08:03

dspjm


People also ask

What is the primary key of Django user model?

Django by default makes a primary key field on each model named " id ", with a type of AutoField .

What is id in Django model?

By default, Django adds an id field to each model, which is used as the primary key for that model. You can create your own primary key field by adding the keyword arg primary_key=True to a field. If you add your own primary key field, the automatic one will not be added.

What is PK in Django model?

pk is short for primary key, which is a unique identifier for each record in a database. Every Django model has a field which serves as its primary key, and whatever other name it has, it can also be referred to as "pk".

Is PK the same as ID Django?

It doesn't matter. pk is more independent from the actual primary key field i.e. you don't need to care whether the primary key field is called id or object_id or whatever. It also provides more consistency if you have models with different primary key fields.


1 Answers

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.

like image 61
Ignacio Vazquez-Abrams Avatar answered Oct 08 '22 12:10

Ignacio Vazquez-Abrams