Example I'm trying to emulate: https://books.agiliq.com/projects/django-admin-cookbook/en/latest/filtering_calculated_fields.html
FieldError at /admin/gallery/galleryitem/ Cannot resolve keyword 'on_sale' into field.
Choices are: content_type, content_type_id, depth, description, direct_sale, direct_sale_extra_description, direct_sale_price, draft_title, expire_at, expired, external_links, first_published_at, formsubmission, gallery_images, go_live_at, group_permissions, has_unpublished_changes, id, index_entries, last_published_at, latest_revision_created_at, live, live_revision, live_revision_id, locked, locked_at, locked_by, locked_by_id, numchild, owner, owner_id, page_ptr, page_ptr_id, path, redirect, revisions, search_description, seo_title, show_in_menus, sites_rooted_here, slug, stock, title, url_path, view_restrictions, workflow_states, workflowpage
models.py
class GalleryItem(Page):
parent_page_types = ['InstallationPage']
description = models.CharField(blank=True, max_length=250)
direct_sale = models.BooleanField("On Sale", default=False, help_text="Check this box to list this item for sale directly on your website.")
direct_sale_price = models.DecimalField("Sale price, $", blank=True, null=True, max_digits=6, decimal_places=2, help_text="Add more info about this item for the store page only.")
direct_sale_extra_description = models.CharField("Addtional sale description (optional)", blank=True, max_length=250, )
stock = models.IntegerField("Number in stock", blank=True, null=True,)
def external_sale(self):
return bool(self.external_links.count())
def on_sale(self, obj):
return obj.external_sale or obj.direct_sale
class ExternalLink(Orderable):
gallery_item = ParentalKey(GalleryItem, on_delete=models.CASCADE, related_name='external_links', help_text="Add details about the listing, ex. dimensions, framing.")
description = models.CharField(blank=True, max_length=250)
external_sale_url = models.URLField(blank=True, help_text="Add an external link to sell this.")
wagtail_hooks.py
class OnSaleFilter(SimpleListFilter):
title = 'On Sale'
parameter_name = 'on_sale'
def lookups(self, request, model_admin):
return (
('Yes', 'Yes'),
('No', 'No'),
)
def queryset(self, request, queryset):
value = self.value()
if value == 'Yes':
return queryset.filter(on_sale=True)
elif value == 'No':
return queryset.filter(on_sale=False)
return queryset
class GalleryItemAdmin(ThumbnailMixin, ModelAdmin):
model = GalleryItem
menu_label = 'All Gallery Items' # ditch this to use verbose_name_plural from model
menu_icon = 'pick' # change as required
menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
add_to_settings_menu = False # or True to add your model to the Settings sub-menu
exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
list_display = ('title', 'admin_thumb', 'description', 'direct_sale', 'external_sale')
list_filter = (OnSaleFilter, InstallationFilter,)
list_per_page = 50
search_fields = ('title', 'description')
thumb_image_field_name = 'main_image'
thumb_image_width = 100
Just add the calculations to a queryset. Example is for news > status:
Or maybe: