title: Django CBV how to handle different pk value on 2 models?
body: <p>Having difficulty Django CBV DetailView to be able to incoporate with Create,Update&amp; Delete View:
  I have two models, the 2nd model have a ForeignKey from the first one: How can I differentiate their pk value on their urls.py. I encounter error:404 when trying to detailView of the pk on the 2nd model. Thank you very much.</p>
  <h1>Models:</h1>
  <pre><code>class Rtana(models.Model):
      rsc_name = models.CharField(max_length=128,unique=True)
      rsc = models.PositiveIntegerField(unique=True)
      cc = models.CharField(max_length=32,unique=True)
      def __str__(self):
          return '%s %s' % (self.rsc_name , self.rsc)
      def get_absolute_url(self):
          return reverse(&quot;app7_cbv:details&quot;,kwargs={'pk':self.pk})
  class Rt(models.Model):
          rt_name = models.CharField(max_length=128,unique=True)
          rsc_no = models.ForeignKey(Rtana,related_name=&quot;route&quot;,on_delete=models.CASCADE,)
          rt_no = models.PositiveIntegerField(unique=True)
          def __str__(self):
              return self.rt_name
          def get_absolute_url(self):
              return reverse(&quot;app7_cbv:rtdetails&quot;,kwargs={'pk':self.pk})
  </code></pre>
  <h1>Urls:</h1>
  <pre><code>app_name = &quot;app7_cbv&quot;
  urlpatterns = [
      url(r'^list$',views.Rtana_List_View.as_view(),name=&quot;list&quot;),
      url(r'^(?P&lt;pk&gt;\d+)/$',views.Rtana__Detail_View.as_view(),name=&quot;details&quot;),
      url(r'^create/$',views.Rtana_Create_View.as_view(),name=&quot;create&quot;),
      url(r'^update/(?P&lt;pk&gt;\d+)/$',views.Rtana_Update_View.as_view(),name=&quot;update&quot;),
      url(r'^delete/(?P&lt;pk&gt;\d+)/$',views.Rtana_Delete_View.as_view(),name=&quot;delete&quot;),
      url(r'^rtlist$',views.Rt_List_View.as_view(),name=&quot;rtlist&quot;),
      url(r'^(?P&lt;pk&gt;\d+)/$',views.Rt_Detail_View.as_view(),name=&quot;rtdetails&quot;),
      url(r'^rtcreate/$',views.Rt_Create_View.as_view(),name=&quot;rtcreate&quot;),
  ]
  </code></pre>
  <h1>views.py:</h1>
  <pre><code>def index(request):
      return render(request,&quot;index.html&quot;)
  class Rtana_List_View(ListView):
      model = models.Rtana
      context_object_name = &quot;rtana_list&quot;
  class Rtana__Detail_View(DetailView):
      model = models.Rtana
      context_object_name = &quot;rtana_detail&quot;
      template_name = &quot;app7_cbv/rtana_detail.html&quot;
  class Rt_List_View(ListView):
      model = models.Rt
      context_object_name = &quot;rt_list&quot;
  class Rt_Detail_View(DetailView):
      model = models.Rt
      context_object_name = &quot;rt_detail&quot;
      template_name = &quot;app7_cbv/rt_detail.html&quot;
  class Rtana_Create_View(CreateView):
      model = models.Rtana
      fields = ('rsc_name','rsc','cc')
  class Rtana_Update_View(UpdateView):
      model = models.Rtana
      fields = &quot;__all__&quot;
  class Rtana_Delete_View(DeleteView):
      model = models.Rtana
      success_url = reverse_lazy(&quot;app7_cbv:list&quot;)
  class Rt_Create_View(CreateView):
      model = models.Rt
      fields = &quot;__all__&quot;
  </code></pre>