1

I'm new to Python and trying to figure out Django 1.3's class-based generic views. Right now, I have the following view which gets a list of Location objects in a Category:

class category_detail(ListView):
    """Return a generic view of locations in a category."""

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context.
        context = super(category_detail, self).get_context_data(**kwargs)
        # Add the current category to the context.
        category = get_object_or_404(Category, slug=self.kwargs['slug'])
        context['category'] = category
        return context

    def get_queryset(self):
        category = get_object_or_404(Category, slug=self.kwargs['slug'])
        return Location.objects.filter(category=category)

It does what I want it to do. But you can see that I'm repeating myself by defining category twice. Is there a way I can add a new property to the class called category that I define once at the top, and then just reference self.category in get_queryset() and get_context_data()?

3 Answers 3

3

I think you should approach it from a different angle: you should not be using ListView showing the Locations of a Category but a DetailView of a Category that also includes the Locations of that category. The name of your view class also suggests that you're showing a detail view of a category. I think it should look more like this:

class CategoryLocationsView(DetailView):
    model = Category
    context_object_name = 'category'

    def get_context_data(self, **kwargs):
        context = super(CategoryLocationsView, self).get_context_data(**kwargs)
        context['location_list'] = self.get_object().location_set.all()
        return context

You now have both the category and the list of locations in your context which you can use in the template.

2
  • +1. Also, you can simplify the code further by dropping the get_context_data method entirely, and put {% with location_list=category.location_set.all %} in the template. Mar 15, 2012 at 20:38
  • This makes much more sense than what I was trying to do. Thanks! Mar 15, 2012 at 21:46
2

Just assign category to self. The only caveat is that you need to be a little careful about where you do that, because some methods are called before others. However, get_queryset is one of the first things activated on a view, so it'll work fine there:

def get_queryset(self):
    self.category = get_object_or_404(Category, slug=self.kwargs['slug'])
    return Location.objects.filter(category=self.category)

def get_context_data(self, **kwargs):
    # Call the base implementation first to get a context.
    context = super(category_detail, self).get_context_data(**kwargs)
    # Add the current category to the context.
    context['category'] = self.category
    return context

FWIW, this is actually the exact method used by the Django docs on class-based views (third code sample down).

1

use the @property decorator

@property
def category(self):
    return get_object_or_404(Category, slug=self.kwargs['slug'])

inside your class and then you can access it as self.category, without the decorator it would be accessible with self.category()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.