Django — Get an objects Primary Key

Somebody asked on #Django IRC: what is an easy way to get an objects primary key? I was shocked to hear he couldn’t find it on google.. and I guess it’s true. This is probably the very first stepping stone in learning Django.

I say: Do the Django Tutorials, they are short, well written, and fun!
Besides, its got a hassle-free return policy (90 days).

Well, uh…
object_instance.id = your primary key

Wait… I was wrong. here’s an update from SmileyChris:

Actually, object_instance.pk would be more correct. “id” is just the default primary key field, whereas “pk” is an alias to the field which is defined as the primary key.

5 Comments

  1. SmileyChris says:

    Actually, object_instance.pk would be more correct. “id” is just the default primary key field, whereas “pk” is an alias to the field which is defined as the primary key.

  2. Lee says:

    Thank you! very helpful!

  3. Jeremiah says:

    Hey there, You’ve done an excellent job. I’ll definitely digg it and
    personally recommend to my friends. I am confident they
    will be benefited from this site.

  4. A diamond engagement ring is the must-have clinhcher for a man wanting to hear her say “Yes. It is universal fact that most of the women of the world love to receive a beautiful diamond engagement rings on her very special and auspicious occasion that is engagement for which she would have shape beautiful dreams and also planned for many thing for that particular occasion. Inspired by these classic masterpieces the Heart shaped diamond engagement rings are available in enthralling red, blue and pure colorless beauty.

  5. For its open nature, any gem in a tension setring will show its beauty but also its flaws.
    ‘ This diamond evokes a vintage feel from the 1920s or 1930s.
    Diamond solitaire rings encapsulate that magic of love.

Leave a reply to SmileyChris Cancel reply