英文:
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'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论