2

For example, I want to build an ecommerce website using wagtail, one component is order. I think order should not wagtail Page, but simple Django model, see code below.

from django.db import models

from wagtail.admin.edit_handlers import (
    FieldPanel,
    StreamFieldPanel,
    MultiFieldPanel,
    InlinePanel,
)
from wagtail.core.fields import RichTextField
from wagtail.core.models import Page

# Since product details are shown to users, so need to have a Page for it.
class XyzProductPage(Page):
    template = "product/product_page.html"

    name = models.CharField(max_length=100)
    desc = RichTextField(blank=False, null=True)

    content_panels = Page.content_panels + [
        FieldPanel("name"),
        FieldPanel("desc"),
    ]

class XyzOrderedProduct(models.Model):
    product = models.ForeignKey(
        "XyzProductPage", on_delete=models.CASCADE, related_name="+"
    )
    order = models.ForeignKey(
        "XyzOrder", on_delete=models.CASCADE, related_name="ordered_products"
    )

# Orders are not shown to users, only for internal use, so use Django model
class XyzOrder(models.Model):
    panels = [
        # and per each order, I want to display all ordered products on wagtail admin UI,
        # so I try to use this MultiFieldPanel, but doesn't seem to work, why?
        MultiFieldPanel(
            [InlinePanel("ordered_products", label="Ordered Product",)],
            heading="Ordered Product(s)",
        ),
    ]

I also defined ModelAdmin for the above Django models, so I can see them on wagtail admin UI.

The questions I have are

  1. When to use wagtail Page model, when to use Django model? In the above example, orders are defined as Django models, or I should use Page?

  2. How to properly use panels for Django models? I saw some tutorial that I could use panels for Django models, but in the above code, I want to list all ordered products in each order (i.e. XyzOrder) on wagtail admin UI, but it doesn't work.

  3. How to select multiple orders and bulk delete them? Looks like wagtail admin has no support for bulk select & delete for Django models, but Django admin has. So how could we do bulk select & delete?

avocado
  • 2,615
  • 3
  • 24
  • 43
  • Hi there! First of all, I'm wondering why you decided to use Wagtail at all. There are a lot of other, and sometimes even better ways to create a webshop type website with Django. Were there any specific requirements that led you to Wagtail? – Nico Griffioen Jun 29 '20 at 07:09
  • @NicoGriffioen Hey, thanks for asking! TBH I'm not sure if wagtail is a good fit for ecom website, I wanted to have a blog on the ecom and found wagtail. Any other alternatives? – avocado Jun 29 '20 at 08:13
  • 1
    It's very much possible to combine good old Django with a Wagtail blog. You can create Django models for your products/orders etc. and use Wagtail only for content that would require a CMS. I would advise you to build a regular Django webshop first (Maybe look into something like [django-oscar](https://github.com/django-oscar/django-oscar)). Then, later on, it's fairly simple to integrate Wagtail into your existing website. See [this](https://docs.wagtail.io/en/v2.0/getting_started/integrating_into_django.html) part of the wagtail docs. – Nico Griffioen Jun 29 '20 at 08:33
  • @NicoGriffioen, thanks for much for suggestions. – avocado Jun 29 '20 at 08:48

1 Answers1

0

While this question is old, it is a good question to give an update to in 2023.

Over the last year, Wagtail has been migrating away from the contrib ModelAdmin (Wagtail's, not Django's) to better first class support for the Snippets system.

Essentially, Snippets are any non-Page model that you want to easily manage, access, report on or even just view within the Wagtail admin. They now support registration against models outside of your main application such as third party packages.

Additionally, there are a lot of features such as Publishing, Revisions, bulk changes (not editing, but deleting and others), audit logs, permissions systems and advanced Panel editing.

This puts Pages and 'any model' almost equivalent in terms of features.

When it comes to picking whether your model should be a Page or a non-Page Snippet the only real consideration now will be how you want the content within the models to be managed. If it's a page like thing, heirachial, has a distinct URL slug, has a Title etc, then a Page would make sense.

Otherwise, Snippets, registered against a standard Django model can be used without too many downsides.

A reminder that even if you want users to access your other models at a URL you can use the RouteablePageMixin to serve data from other models.

Finally, whether you should be using a CMS for an e-commerce website is something to consider carefully. There are overlaps but each is its own complex space and you should always understand your use case before picking a tool. Thankfully Wagtail strives to be 'just Django' so integration with a Django e-commerce framework should always be possible, but maybe not simple.

Some useful links:

LB Ben Johnston
  • 4,751
  • 13
  • 29