Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,862 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