Using Multiple URL Parameters to get_object in Class-Based-View
<p>Alright, I'm fairly new to this, I've been working on my Project for a couple months now and I'd like to create URLs that accept multiple parameters to call a View. A sample URL would look like this:
<a href="http://www.sample.com/builders/" rel="nofollow noreferrer">http://www.sample.com/builders/</a>/m//</p>

<p>I've got this implemented successfully, by overriding get_object in my DetailView, but I'm wondering if there is a better/easier method for accomplishing this or if this is considered a bad practice. Any guidance would be appreciated.</p>

<p>urls.py</p>

<pre><code>urlpatterns = [
    # url(r'^$', builder_list, name='list'),
    # url(r'^create/$', builder_create, name='create'),
    # url(r'^(?P&lt;slug&gt;[\w-]+)/$', builder_detail, name='detail'),
    # url(r'^(?P&lt;slug&gt;[\w-]+)/edit/$', builder_update, name='update'),
    # url(r'^(?P&lt;slug&gt;[\w-]+)/delete/$', builder_delete, name='delete'),

    # url(r'^$', builder_list, name='sub_list'),
    # url(r'^m/create/$', sub_create, name='sub_create'),
    url(r'^(?P&lt;builder&gt;[\w-]+)/m/(?P&lt;market&gt;[\w-]+)/$', sub_detail, name='sub_detail'),
    # url(r'^m/(?P&lt;slug&gt;[\w-]+)/edit/$', sub_update, name='sub_update'),
    # url(r'^m/(?P&lt;slug&gt;[\w-]+)/delete/$', sub_delete, name='sub_delete'),
]
</code></pre>

<p>views.py</p>

<pre><code>class BuilderSubDetailView(DetailView):
    model = BuilderSub
    template_name = "builders/sub_detail.html"

    def get_context_data(self, **kwargs):
        context = super(BuilderSubDetailView, self).get_context_data(**kwargs)
        context['now'] = timezone.now()
        print(context)

        return context

    def get_object(self, queryset=None):
        if queryset is None:
            queryset = self.get_queryset()

        # Next, try looking up by primary key.
        builder = self.kwargs['builder']
        builder_id = Builder.objects.filter(slug=builder).first().pk
        market = self.kwargs['market']
        market_id = Market.objects.filter(slug=market).first().pk
        if builder is not None and market is not None:
            queryset = BuilderSub.objects.filter(parent=builder_id).filter(market=market_id)

        # If none of those are defined, it's an error.
        if builder is None or market is None:
            raise AttributeError("Generic detail view %s must be called with "
                                 "Builder and Market"
                                 % self.__class__.__name__)
        try:
            # Get the single item from the filtered queryset
            obj = queryset.get()
        except queryset.model.DoesNotExist:
            raise Http404("No %(verbose_name)s found matching the query") % \
                {'verbose_name': queryset.model._meta.verbose_name}
        return obj
</code></pre>

<p>And models.py for reference -- also is there any problem with my get_absolute_url function?</p>

<pre><code>class Builder(models.Model):
    added_by = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    company_name = models.CharField(max_length=80, help_text="Full Company Name", unique=True)
    short_name = models.CharField(help_text="Short Company Name", max_length=30)
    slug = models.SlugField(unique=True)
    website = models.CharField(max_length=80, help_text="Format: www.[website].com")
    logo = models.ImageField(blank=True, null=True)
    timestamp = models.DateTimeField(auto_now_add=True)
    info = RedactorField(verbose_name=u'Company Info')

    def show_website_url(self):
        return format_html("&lt;a href='{url}'&gt;{url}&lt;/a&gt;", url=self.website)

    def __str__(self):
        return self.short_name


class BuilderSub(models.Model):
    parent = models.ForeignKey(Builder)
    market = models.ForeignKey(Market, null=True, blank=True)
    details = RedactorField(verbose_name=u'Details', blank=True, null=True)
    main_contact = models.ForeignKey(Person, blank=True, null=True)

    def __str__(self):
        return "{}: {} - {}".format(self.pk, self.market.name, self.parent.short_name)

    def get_absolute_url(self):
        return reverse('builders:sub_detail', kwargs={'market': self.market.slug, 'builder': self.parent.slug})


def pre_save_builder_reciever(sender, instance, *args, **kwargs):
    instance.slug = slugify(instance.short_name)

pre_save.connect(pre_save_builder_reciever, sender=Builder)
</code></pre>

<p>I'm not 100% sure I'm my BuilderSub Model is the appropriate way to handle the Relationship between the overall Builder (company) and the Markets they serve so any guidance there would be appreciated as well.</p>