问题描述
我试图模仿的例子: 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,深度,描述,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,last_at,ed_ed,ed_at,ed_ed,ed_at,ed_at,ed_ed,ed_reed, live_revision,live_revision_id,locked,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,stock,title,surl_path,page_re状态/ p>
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)",max_length=250,)
stock = models.IntegerField("Number in stock",)
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,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
解决方法
只需将计算添加到查询集。 示例是新闻>状态:
#_____________________________________________________________________________
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