我不确定为什么这会导致我这样的问题,但我似乎无法弄明白。我的Mezzanine / Cartridge产品数据库中的产品有CSV和PDF导出。它会连续导出每个ProductVariation。工作得很好,但我需要添加一个过滤器,例如只导出已发布的产品。 ProductVariations与产品型号具有外键关系:
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')
但它只是给了我一个“'bool'对象不可迭代”的错误。我做错了什么?
答案 0 :(得分:0)
当你写:
ProductVariation.objects.filter('product__status' == 'CONTENT_STATUS_PUBLISHED')
表达式'product__status' == 'CONTENT_STATUS_PUBLISHED'
变为布尔值。
正确的语法是:
ProductVariation.objects.filter(product__status = 'CONTENT_STATUS_PUBLISHED')