DjangoCMS子插件,用于显示从DB选项卡过滤的数据

2024-10-03 04:25:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个插件ProductSelector(parent)和SpecificationSelector(child)。我想设置子级,以便当您将其添加到父级时,所显示的规范是产品(父级)的唯一规范。现在它从表格中提取了所有的规格。这些行允许我过滤数据以得到我想要的。在

编辑:我发现了一个我在代码中修复的错误。我把PluginBase的名字和模型的名字一样。这让我可以产品选择器.objects.get(cmsplugin-u-ptr)=实例.父级)以获取父实例。我仍然需要弄清楚如何将过滤后的规范列表传递到“PluginAdmin接口”

    product = ProductSelector.objects.get(cmsplugin_ptr=instance.parent)
    specification = Specifications.objects.filter(product_name__product_name__iexact = product.product_name)

但是,我还没有弄清楚如何将过滤后的列表发送到插件管理接口。在

^{pr2}$

在模型.py在

class ProductSelector(CMSPlugin):
    product_name = models.ForeignKey(Product, help_text = "Select the product you want to place")
    new_product = models.BooleanField(blank=True)


class SpecificationSelector(CMSPlugin):
    specification = models.ManyToManyField(Specifications, blank=True)

    def __unicode__(self): 
        return unicode(self.specification)

这是占位符中Django cms插件的屏幕截图。目前它显示了表中的所有规格,但我只希望它是特定产品的规格。在

http://imgur.com/3R1LobC

提前谢谢你的帮助。在


Tags: name模型规范插件objects产品modelsproduct
1条回答
网友
1楼 · 发布于 2024-10-03 04:25:51

CMSPluginBase来自ModelAdmin,这意味着您可以在添加和编辑插件时重写呈现的表单。在

因此您可以创建一个ModelForm子类,如下所示:

class SpecificationSelectorPluginForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(SpecificationSelectorPluginForm, self).__init__(*args, **kwargs)
        if self.instance.parent_id:
            # Assume that the parent is a product instance
            parent_plugin = self.instance.parent
            product = parent_plugin.get_plugin_instance()[0]

            if product:
                # It's possible that product is an orphan plugin.

                specifications = Specifications.objects.filter(
                    product_name__product_name__iexact=product.product_name)
                self.fields['specification'].queryset = specifications

然后将您的SpecificationSelectorPlugin改为如下形式:

^{pr2}$

只有当规范插件是产品插件的直接子级时,上述方法才有效。在

相关问题 更多 >