2012-10-24 136 views
0

我在Ubuntu 12.04上使用Python 2.7的Django 1.4。Django模型过滤器不拉对象

我有一个模板,应该显示产品和每个产品的产品功能列表,并出于某种原因功能不显示在模板中。

这里是视图:

@login_required 
def view_products(request): 
    """ 
    .. function:: view_products() 

     View the Products 

     :param request: Django Request object 
    """ 

    data = { 'user' : request.user } 
    if (request.user.is_authenticated() and request.user.is_superuser): 
     products = Products.objects.all() 

     add_feature_form = rsb.forms.AddProductFeatureForm(); 
     data.update({ 'form' : add_feature_form }) 
     data.update({ 'products' : products }) 
     data.update(csrf(request)) 

     return render_to_response("view_products.html", data) 

    return render_to_response("index.html", data) 

这里是模板,以产品功能的工作的一部分:

<table> 
    {% for product in products %} 
     <tr> 
      <td align="right">Product Name:</td><td>{{ product.name }}</td> 
     </tr> 
     <tr> 
      <td align="right">Price:<br /></td><td>${{ product.price }}</td> 
     </tr> 
     <tr> 
      <ul> 
      {% for productfeature in product.productfeature_set.all %} 
       <form action="/removeProductFeature/" method="post">{% csrf_token %} 
       <li> 
        {{ productfeature.feature }} 
        <input type="hidden" name="feature" value={{ productfeature.feature }}> 
        <input type="hidden" name="product_id" value={{ product.id }}> 
        <label class="formlabel">&nbsp;</label><input type="submit" value="Remove &#9658;"> 
       </tr> 
       </form> 
      {% endfor %} 
      </ul> 
     </tr> 
     <tr> 
      <form action="/addProductFeature/" method="post">{% csrf_token %} 
      <table> 
       <tr> 
        <td align="right"><label class="formlabel">Add Feature:<br /></label></td><td>{{ form.feature }}</td> 
       </tr> 
       <input type="hidden" name="product_id" value={{ product.id }}> 
       <tr> 
        <td align="right"><label class="formlabel">&nbsp;</label></td><td><input type="submit" value="Add &#9658;"></td> 
       </tr> 
      </form> 
      </table> 
     </tr> 
    {% endfor %} 
</table> 

基本上这个模板会显示出产品。每个功能都将在下方列出,并可选择“删除”该功能。然后,在底部,可以添加其他功能的字段。

现有的功能根本不显示。有关我可能会做错什么的建议?

更新1:

我错过了在模板中sproduct.productfeatures_set.all不是product.productfeature_set.all。我很好走。谢谢大家!

+0

我可以告诉你是什么导致了错误:'.filter()'返回一个'QuerySet',因此你的'product_features'列表是'QuerySet'对象的集合;但是你需要重新思考你对问题的态度。查看[跨越关系的查找](https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships) –

回答

3

请不要这样做:

product_features = [] 
for product in products: 
    features = ProductFeatures.objects.filter(product = product) 
    product_features.append(features) 
    product.features = product_features 

相反,只要你的产品变量传递给模板上下文。

而且在模板做:

{% for product in products %} 
    Product id: {{ product.pk }} 
    {% for productfeature in product.productfeature_set.all %} 
     {{ productfeature.feature }} 
    {% endfor %} 
{% endfor %} 

什么是productfeature_set你会问(或者,我希望你会问:d),这是一个很好的问题。不要惊慌,这全是documented

现在,这将导致子查询产生。解决方案是使用prefetch_related。但你现在不用担心这一点:)

+0

我已经做出了您所建议的更改 - 并且简化了它我的观点很大 - 但我仍然无法在模板中看到该功能。我会更新我原来的问题。 – Rico

+0

另外,关于'prefetch_related',我已经用其他视图/模板完成了它,但并不认为这是我在这里所做的事情所必需的。我只有3种产品,每种都有6个功能。 :) – Rico

1

是的,你正试图实现已经构建到Django中的东西!

你应该使用django的反向关系构建。

# give an Product instance product 
# this should work 
product.productfeature_set.all() 

这是从模板product.productfeature_set.all 访问,可以在itterated。