0

だから私は、私がやろうとして失敗したことのこの単純化されたバージョンを持っています。色フィールドを持つこの Boxes オブジェクトがあり、m2m フィールドによって多くのアイテムを持つことができるので、特定の色のボックスにあるすべてのアイテムのリスト (クエリセット) が必要です。そこで、管理者用にこの django フィルターを作成しました。

from django.contrib.admin import SimpleListFilter
from store.models import Box

class Items(Model):
    name = CharField(max_length=200)

class Box(Model):
    items = ManyToManyField(Items)
    color_choices = (
        ('yellow','yellow'),
        ('green','green'),
    )
    box_color = CharField(max_length=200,choices=color_choices)

# So in my Items admin list i want to filter the items by color, this is my 'failed' attemp.

class Items_by_payment_system_filter(SimpleListFilter):
    title = _('Colors')

    # Parameter for the filter that will be used in the URL query.
    parameter_name = 'color'

    def lookups(self, request, model_admin):
        """
        Returns a list of tuples. The first element in each
        tuple is the coded value for the option that will
        appear in the URL query. The second element is the
        human-readable name for the option that will appear
        in the right sidebar.
        """
        return Box.color_choices
    def queryset(self, request, queryset):
        # Remember that self.value contains the current selection in the filter options displayed to the user in Admin
        if self.values():
            boxes = Box.objects.filter(items__in=queryset) # get only boxes with the current set of items
            boxes_ids = boxes.filter(box_color=self.values()).values_list('items__id',flat=True)
            return queryset.filter(id__in=boxes_ids)

管理者が、選択した色とは異なる色のボックスに属するアイテムを表示しているため、何が問題なのかわかりません。

4

1 に答える 1

1

これはあなたの問題です:

  • に変更self.values_self.valueif self.values():
  • に変更color_box_colorboxes_ids = boxes.filter(color=self.values()) ...

コード例 (django 1.4.2 でのテストと動作)

from box.models import Items,Box
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin import SimpleListFilter

class Items_by_payment_system_filter(SimpleListFilter):
    title = _('Colors')

    # Parameter for the filter that will be used in the URL query.
    parameter_name = 'color'

    def lookups(self, request, model_admin):
        """
        Returns a list of tuples. The first element in each
        tuple is the coded value for the option that will
        appear in the URL query. The second element is the
        human-readable name for the option that will appear
        in the right sidebar.
        """
        return Box.color_choices
    def queryset(self, request, queryset):
        # Remember that self.value contains the current selection in the filter options displayed to the user in Admin

        if self.value():
            boxes = Box.objects.filter(items__in=queryset) # get only boxes with the current set of items
            boxes_ids = boxes.filter(box_color=self.value()).values_list('items__id',flat=True)
            return queryset.filter(id__in=boxes_ids)


class ItemsAdmin(admin.ModelAdmin):
    fields = ['name']
    list_filter = (Items_by_payment_system_filter,)

admin.site.register(Items,ItemsAdmin)
admin.site.register(Box)
于 2012-12-04T08:38:26.237 に答える