Overriding get_queryset() in a Django DetailView

thesteve picture thesteve · Jun 19, 2011 · Viewed 19.3k times · Source

I have two models, City and State with State being a ForeignKey relation of City.My CityDetailView url is constructed as:

r'^state/(?P<state>[-\w]+)/city/(?P<slug>[-\w]+)/$'

My CityDetailView called by the above url is:

class CityDetailView(DetailView):
    model = City
    context_object_name = 'city'
    template_name = 'location/city_detail.html'

    def get_queryset(self):
        state = get_object_or_404(State, slug__iexact=self.kwargs['state'])
        return City.objects.filter(state=state)

    def get_context_data(self, **kwargs):
        context = super(CityDetailView, self).get_context_data(**kwargs)
        city = City.objects.get(slug__iexact=self.kwargs['slug'])
        context['guide_list'] = Guide.objects.filter(location=city).annotate(Count('review'), Avg('review__rating'))
        return context

My City model has unique Names for each city. If I try and access a city that occurs in two states I get an error that the get() returned more than one City. I am trying to override the get_queryset() method to filter out only the City models in a single state but it does not seem to be working which is odd because my CityListView is similar but works fine. Any thoughts on what I am missing would be appreciated.

Answer

Valder Gallo picture Valder Gallo · Dec 24, 2013

You need to override the method get_object in DetailView to do this.

Something like this should do:

class CityDetailView(DetailView):
    model = City
    context_object_name = 'city'
    template_name = 'location/city_detail.html'

    def get_object(self):
        state = get_object_or_404(State, slug__iexact=self.kwargs['state'])
        return self.model.objects.filter(state=state)

    def get_context_data(self, **kwargs):
        context = super(CityDetailView, self).get_context_data(**kwargs)
        cities = self.object
        context['guide_list'] = Guide.objects.filter(location=cities).annotate(Count('review'), Avg('review__rating'))
        return context