2014-05-19 71 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