Search code examples
pythonmysqldjangodjango-southattributeerror

Python AttributeError - 'Category' object has no attribute 'product_set'


My show_category view for my ecommerce catalog was fully functional yesterday. Navigating to domain.com/category/necklaces, for example, would call the show_category view and display all my products that are in the necklaces category. This is accomplished with two tables, products and categories, and their associated "through" tables.

I HAD been using just syncdb, but I had to add a new ManyToManyField, and so Django South got involved, I installed that, recreated the database, used django south to manage my migrates, got everything set up and added my categories and products back in the django admin, which all went smoothly. I checked the database itself in SQL Workbench, and it all looks grand - a perfect recreation of what I had last night, but now using Django South.

But then I navigated to domain.com/category/necklaces where I expected to see my category page listing my one test necklace product. Instead I got this:

AttributeError at /category/necklaces/
'Category' object has no attribute 'product_set'

The error is occurring here, in my catalog.views.py:

def show_category(request, 
                  category_slug, 
                  template_name="catalog/category.html"):
    c = get_object_or_404(Category, 
                          slug=category_slug)
    products = c.product_set.all()

But the thing is, this didn't change AT ALL since last night. I checked the database and YES the product exists, and YES the "through" table is correctly hooking up that product to this category.

Any ideas where I should look? I ASSUME the error is somewhere in the database, since I basically didn't touch the then-working code, but the database looks so pristine it is hard to say. Any and all help is appreciated. I'll be poking around myself in the meantime.


Solution

  • Adding the comment as an answer for future reference:

    If you have used a related_name in the foreign key reference, _set would no longer work, since django provides _set as a default related name, and the custom related name takes precedence

    Some documentation on usage of related_name here