class warehousePeopleBase(ModelAdmin): model = warehousePeople menu_label = '库房人员出入库信息' # ditch this to use verbose_name_plural from model menu_icon = 'site' # change as required list_display = ['name', 'yearMonth', 'month', 'warehouse', 'subjectMatter'] list_filter = ('name', 'yearMonth', 'month', 'warehouse',) 如何将代中list_filter过滤器中的yearMonth字段改为可以选择你那份和月份的过滤器?
时间: 2023-12-21 19:05:10 浏览: 132
Designing A Common Sense Approach to Web & Mobile Application Design
你可以按照以下步骤对`yearMonth`字段进行改造,使其能够按照年份和月份进行筛选:
1. 首先,你需要安装`django-admin-range-filter`库,该库提供了一种方便的方式,可以在list_filter中添加日期范围过滤器。
```python
pip install django-admin-range-filter
```
2. 在`warehousePeople`模型中,将`yearMonth`字段改为`DateField`类型,并添加一个`@property`方法,用于获取该字段的年份和月份。
```python
from django.db import models
from django.utils import timezone
class warehousePeople(models.Model):
name = models.CharField(max_length=30)
year_month = models.DateField(default=timezone.now)
warehouse = models.CharField(max_length=30)
subject_matter = models.CharField(max_length=30)
@property
def year(self):
return self.year_month.year
@property
def month(self):
return self.year_month.month
```
3. 在`warehousePeopleBase`中,将`yearMonth`字段改为`RangeFilter`类型,并指定其对应的年份和月份。
```python
from admin_range_filter.filters import DateRangeFilter
class warehousePeopleBase(ModelAdmin):
model = warehousePeople
menu_label = '库房人员出入库信息'
menu_icon = 'site'
list_display = ['name', 'year_month', 'warehouse', 'subject_matter']
list_filter = (
('year_month', DateRangeFilter),
('year', admin.RelatedOnlyFieldListFilter),
('month', admin.RelatedOnlyFieldListFilter),
('warehouse', admin.RelatedOnlyFieldListFilter),
)
```
这样,就可以在wagtail的后台管理界面中,使用`DateRangeFilter`对`yearMonth`字段按年份和月份进行筛选了。同时,还可以通过`RelatedOnlyFieldListFilter`对`year`、`month`和`warehouse`字段进行筛选。
阅读全文