tags:

views:

63

answers:

1

I have code

c = get_object_or_404(Category, slug=category_slug) 
products = c.product_set.all() 
page_title = c.name

Code doesn't work with error

'Category' object has no attribute 'product_set'

What wrong? I can't found any description about "product_set" on django.org

+2  A: 

I assume you might changed related name for Product, correct?

class Product(Model):
   category = models.ForeignKey(Category, related_name='foobar')

OR did you set FK from you product model to category at all ?

Pydev UA
Of course, you are absolutely right ...
App Categoryclass Category(models.Model): name = models.CharField(max_length=50) App Goodsclass Goods(models.Model): categories_good = models.ForeignKey(Category, db_column='categories_good', related_name='categories', to_field='name') In category.views.py def show_category(request, category_slug, template_name="catalog/category.html"): c = get_object_or_404(Category, slug=category_slug) products = get_object_or_404(Goods, categories_good = c.name) products = c.product_set.all() It's same "product_set", that caused ERROR...
So.. you have related_name='categories' - which very non logical name (related name is how other models will call this objects)... so if you want your code to work do c.categories.all() BUT better just remove related_name attribute or name it "more correct" (like "products")
Pydev UA