在Django管理界面中如何使用自定义过滤器过滤模型属性值

huangapple go评论56阅读模式
英文:

How to filter the model property value using custom filter in Django admin

问题

根据自定义筛选器的值,我需要在Django管理页面上更新_approved属性的值(而不是模型查询集)。

model.py

class Model(models.Model):
    created = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name='Created')
    updated = models.DateTimeField(auto_now_add=False, auto_now=True, verbose_name='Updated')

    @property
    def _approved(self):
        return ModelRecordRelation.objects.filter(model_id=self.id, status='A').count()

admin.py

class TimeFrameFilter(SimpleListFilter):
    title = 'timeframe'
    parameter_name = 'timeframe'

    def lookups(self, request, model_admin):
        return [
            ('Today', 'Today'),
            ('ThisWeek', 'This Week'),
        ]

    def queryset(self, request, queryset):
        return queryset.filter()

class ModelAdmin(admin.ModelAdmin):
    list_display = ['created', 'updated']

    class Meta:
        model = Model

    def approved(self, obj):
        return obj._approved

    approved.admin_order_field = 'approved'
英文:

I have the model with property and custom filter. Depending on the custom filter value, I need to update the _approved property value (not the model queryset) on the Django admin page.

model.py

class Model(models.Model):
    created = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name='Created')
    updated = models.DateTimeField(auto_now_add=False, auto_now=True, verbose_name='Updated')

    @property
    def _approved(self):
        return ModelRecordRelation.objects.filter(model_id=self.id, status='A').count()

admin.py

class TimeFrameFilter(SimpleListFilter):
    title = 'timeframe'
    parameter_name = 'timeframe'

    def lookups(self, request, model_admin):
        return [
            ('Today', 'Today'),
            ('ThisWeek', 'This Week'),
        ]

    def queryset(self, request, queryset):

        return queryset.filter()


class ModelAdmin(admin.ModelAdmin):
    list_display = ['created', 'updated']

    class Meta:
        model = Model

    def approved(self, obj):
        return obj._approved

    approved.admin_order_field = 'approved'

答案1

得分: 2

你需要在你的管理类中自定义get_queryset方法:

class ModelAdmin(admin.ModelAdmin):
    list_display = ['created', 'updated', 'approved']
    list_filter = [TimeFrameFilter]

    def get_queryset(self, request):
        queryset = super().get_queryset(request)
        queryset = queryset.annotate(approved_count=Count('modelrecordrelation', filter=models.Q(modelrecordrelation__status='A')))
        return queryset

    def approved(self, obj):
        return obj.approved_count

    approved.admin_order_field = 'approved_count'
英文:

You have to customize get_queryset in you admin class:

class ModelAdmin(admin.ModelAdmin):
    list_display = ['created', 'updated', 'approved']
    list_filter = [TimeFrameFilter]

    def get_queryset(self, request):
        queryset = super().get_queryset(request)
        queryset = queryset.annotate(approved_count=Count('modelrecordrelation', filter=models.Q(modelrecordrelation__status='A')))
        return queryset

    def approved(self, obj):
        return obj.approved_count

    approved.admin_order_field = 'approved_count'

huangapple
  • 本文由 发表于 2023年7月13日 16:41:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定