Reputation: 2735
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
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?
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.
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?
Upvotes: 2
Views: 1253
Reputation: 5176
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:
Upvotes: 1