2014-05-19 31 views
0

我不知道为什么这会导致我这样的问题,但我似乎无法弄清楚。我在我们的Mezzanine/Cartridge产品数据库中获得了CSV和PDF导出产品。它连续导出每个ProductVariation。很好的工作,但我需要添加一个过滤器,例如只导出已发布的产品。 ProductVariations有一个外键关系到产品型号:在父类上通过布尔字段过滤queryset

class ProductVariation(Priced): 
""" 
A combination of selected options from 
``SHOP_OPTION_TYPE_CHOICES`` for a ``Product`` instance. 
""" 

product = models.ForeignKey("Product", related_name="variations") 

产品型号子类可显示:

class Product(Displayable, Priced, RichText, AdminThumbMixin): 
""" 
Container model for a product that stores information common to 
all of its variations such as the product's title and description. 
""" 

Displayable类是用来判断产品是否显示普通用户或仅工作人员:

CONTENT_STATUS_DRAFT = 1 
CONTENT_STATUS_PUBLISHED = 2 
CONTENT_STATUS_COMPLETE = 3 
CONTENT_STATUS_INACTIVE = 4 

CONTENT_STATUS_CHOICES = (
    (CONTENT_STATUS_DRAFT, _("Draft")), 
    (CONTENT_STATUS_PUBLISHED, _("Online")), 
    (CONTENT_STATUS_COMPLETE, _("Complete")), 
    (CONTENT_STATUS_INACTIVE, _("Inactive")), 
) 


class Displayable(Slugged, MetaData, TimeStamped): 
""" 
Abstract model that provides features of a visible page on the 
website such as publishing fields. Basis of Mezzanine pages, 
blog posts, and Cartridge products. 
""" 

status = models.IntegerField(_("Status"), 
    choices=CONTENT_STATUS_CHOICES, default=CONTENT_STATUS_DRAFT, 
    help_text=_("The General public can only view content that has ONLINE status.")) 

在尝试按状态过滤结果,但我似乎无法让它按我期望的方式工作。在我的报告视图我将添加像这样:

product_variations = ProductVariation.objects.filter('product__status' == 'CONTENT_STATUS_PUBLISHED') 

,但它只是给了我一个错误,“‘布尔’对象不是可迭代”。我究竟做错了什么?

回答

0

当你写:

ProductVariation.objects.filter('product__status' == 'CONTENT_STATUS_PUBLISHED') 

表达'product__status' == 'CONTENT_STATUS_PUBLISHED'成为一个布尔值。

正确的语法是:

ProductVariation.objects.filter(product__status = 'CONTENT_STATUS_PUBLISHED') 
+0

也就是说完全正确!当我正确访问它时,它正常工作,它正在查看分配的整数字段,因此一旦我将其更改为(product__status ='2'),它就会正确过滤。谢谢! – Neum