通过Wagtail Snippets Inline Objects迭代

时间:2017-02-17 20:17:50

标签: django django-models django-templates django-admin wagtail

我有2个型号的餐厅菜单。 Product模型和MenuSelection模型。产品可以属于许多菜单。 MenuSelection作为内联关系添加到Product

我遇到的挑战是在ModelAdmin list_display中列出菜单对象。我是否需要创建ForeignKey关系或创建迭代Inline对象的@property?根据我的经验,我绝对打了另一面墙。任何帮助将不胜感激。

MenuSelection

class MenuSelection(ClusterableModel):
    menu_section = models.CharField(default=None, max_length=100, choices=MENU_CHOICES, unique=True, verbose_name='Menu Section')
    menu = models.CharField(default=None, max_length=100, choices=MENU, unique=True, verbose_name='Menu')
    menu_price = models.DecimalField(
        blank=True,
        null=True,
        max_digits=5,
        decimal_places=2,
        verbose_name='Menu Price',
        help_text='Numbers only with 2 digital decimal. I.e. 25.00'
    )

    panels = [

        MultiFieldPanel(
            [
                FieldPanel('menu'),
                FieldPanel('menu_section'),
                FieldPanel('menu_price'),
            ],
            heading="Menu & Prices",
            classname="collapsible"
        ),
    ]

产品

class ProductMenuPrices(Orderable, MenuSelection):
    page = ParentalKey('Product', related_name='menu_selection')


@register_snippet
class Product(ClusterableModel):
    product_title = models.CharField(max_length=255, verbose_name='Menu Item')
    product_description = models.TextField(verbose_name='Product Description', blank=True)

    panels = [
        MultiFieldPanel(
            [
                FieldPanel('product_title'),
                FieldPanel('product_description'),
                InlinePanel('menu_selection', label="Menu & Price Assignment", max_num=3),
            ],
            heading="Product Detail",
            classname="collapsible"
        ),
    ]

    class Meta:
        verbose_name = 'Menu Item'

    def __str__(self):
        return self.product_title

ProductModelAdmin

class ProductModelAdmin(ModelAdmin):
    model = Product
    menu_label = 'Menu'
    menu_icon = 'snippet'
    menu_order = 300
    add_to_settings_menu = False
    exclude_from_explorer = False
    list_display = ('product_title', 'product_description')
    list_filter = 'product_title',
    search_fields = 'product_title',


modeladmin_register(ProductModelAdmin)

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

   class Product(ClusterableModel):

       def get_menu_selection(self):
           menu_selection = self.menu_selection.count()
           return menu_selection

    class ProductModelAdmin(ModelAdmin):

        list_display = ('product_title', 'get_menu_selection')

干杯,

罗伯特

答案 1 :(得分:0)

如果你想让它成为wagtail片段管理员的关系,你需要使用他们的方法。

以下是基于我的代码的未经测试的示例,用于您的用例:

@register_snippet
class MenuSelection(ClusterableModel):
    # fields...

    def product_list(self):
        return ', '.join([menuproduct.product.name for menuproduct in self.products.all()])

    panels = [
        # field panels
        InlinePanel('products', label="Products", min_num=1)
    ]


@register_snippet
class Product(ClusterableModel):
    name = models.CharField(max_length=100)
    # fields ...

    @property
    def menu_selections(self):
        MenuSelection.objects.filter(products__in=[self])


class MenuSelectionProduct(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    menu_selection = ParentalKey(MenuSelection, related_name='products')

MenuSelectionProduct定义了ParentalKey related_name。这样你就告诉django ORM在MenuSelection上提供这种关系。 这使您可以在InlinePanel上为此模型创建MenuSelection。 方法product_list是关于如何在MenuSelection对象上进一步处理此关系的示例。 menu_selection上的Product属性可用于解决其他方向的关系。