Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Django Templates Add a Detail View

Dat Ngo
Dat Ngo
1,309 Points

What is pk

I don't understand what pk is? Can anyone help me?

2 Answers

pk is the attribute that contains the value of the primary key for the model.

Alex Koumparos
Alex Koumparos
Python Development Techdegree Student 36,887 Points

To clarify Lazarus's answer, pk is a shortcut to the model field that is the primary key, i.e., it contains a reference to the primary key field, not the primary key value itself.

For example, if you had a model User and specified a primary key with the name user_id, you could query the primary key like this:


Since looking up objects by primary key is so common, Django provides pk as a convenience shortcut so we don't need to remember the actual primary key name. Thus we can write the same query like this:


For more details, you can check out the official Django documentation