0

我试图模仿的例子: https ://books.agiliq.com/projects/django-admin-cookbook/en/latest/filtering_calculated_fields.html

/admin/gallery/galleryitem/ 的 FieldError 无法将关键字“on_sale”解析为字段。

选项有: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_id、live、live_revision ,锁定,locked_at,locked_by,locked_by_id,numchild,所有者,owner_id,page_ptr,page_ptr_id,路径,重定向,修订,search_description,seo_title,show_in_menus,sites_rooted_here,slug,股票,标题,url_path,view_restrictions,workflow_states,workflowpage

模型.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
4

1 回答 1

0

只需将计算添加到查询集中。例如新闻 > 状态:

#_____________________________________________________________________________
class NewsItem(models.Model):
    objects = NewsItemQuerySet.as_manager()


#_____________________________________________________________________________
class NewsItemQuerySet(models.query.QuerySet):

    def with_status_key(self):
        now = timezone.now()
        queryset = self.select_related('parent')
        queryset = queryset.annotate(
            status_key = Case(
                When(go_live_at__gt=now, then=Value(0)), # pending
                When(go_live_at__lte=now, expire_at__gte=now, then=Value(1)), # running
                default=Value(2), # archived
                output_field=IntegerField(),
            )
        ).order_by('status_key', 'go_live_at', 'expire_at')
        return queryset

或者可能:

from wagtail.core.models import PageManager

#_____________________________________________________________________________
class MyManager(PageManager):

    def get_queryset(self):
        return super().get_queryset()

    def with_status_key(self):
        queryset = self. get_queryset().select_related('parent')
        etc
于 2020-09-09T06:31:34.723 回答