How does Django know what 'pk' to use in urls.py?
<p>I've been learning Django over the past couple of weeks, and there is one thing that really seems to confuse me. Which model's attributes does Django use to define the <code>&lt;pk&gt;</code> that is used in <code>urls.py</code>?</p>

<p>For example, If I have:</p>

<pre><code>urlpatterns = [
    url(r'^(?P&lt;pk&gt;\d+)/$', ProductDetailView.as_view(), name="product-detail"),
]
</code></pre>

<p>I had assumed previously that the <code>pk</code> would be derived from the model instance that is being used in the given view, in this case, <code>ProductDetailView.as_view()</code>. However, I'm starting to question that logic as you can pass multiple models into a view, of course.</p>

<h2><strong>Part 2</strong></h2>

<p>Also, what if I wanted to use the <code>pk</code> of one model instance, while <em>only</em> using a <strong>different</strong> model instance in the view? </p>

<p>For example, what if I had two models, <code>Products</code> &amp; <code>Stores</code> which both hold a many-to-many relationship (eg. a product could be in multiple stores, and a store can hold many products). Then I wanted to have a <code>url</code> where I have a <code>StoreListView</code> listing all the stores that hold a given product, so my url would be something like:</p>

<p><code>url(r'^(?P&lt;pk&gt;\d+)/$', StoreListView.as_view(), name="store-list")</code></p>

<p><em>Where the <code>pk</code> is of the <code>Product</code> instance but the <code>view</code> is of the <code>Store</code> instance</em></p>

<p>To finalize the question, again, how does Django define <code>pk</code>?</p>