class collectionStatistic(ModelAdmin): model = CollectionManagement menu_label = '藏品统计' menu_icon = 'form' list_display = ['bar_chart'] def bar_chart(self, obj): # 获取图表数据 data = [10, 20, 30, 40, 50] # 创建图表 fig, ax = plt.subplots() ax.bar(range(len(data)), data) # 设置图表标签和标题 ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_title('Bar Chart') # 将图表转换为HTML字符串 from io import BytesIO buffer = BytesIO() fig.savefig(buffer, format='png') buffer.seek(0) image_png = buffer.getvalue() buffer.close() # 将图表作为HTML字符串返回 from django.utils.html import format_html return format_html('<img src="data:image/png;base64,{}" />', base64.b64encode(image_png).decode()) bar_chart.short_description = '藏品统计' 为什么展示出来的条形统计图的数量是ColllectionManagement表中数据的数量?
时间: 2024-02-14 16:24:26 浏览: 23
展示出来的条形统计图的数量是CollectionManagement表中数据的数量,是因为在该函数中,获取了数据并进行了处理,然后通过使用Matplotlib库生成了一个图表,并将其转换成HTML字符串的形式返回。在这个过程中,没有对数据进行筛选或者处理,因此生成的图表中包含了所有的数据。如果需要展示特定数据的图表,可以在获取数据时进行筛选,或者在函数中添加参数来控制需要展示的数据。
相关问题
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',) wagtail怎么将右侧过滤器的yearMonth字段更改为可以任意的选择年份和月份?
你通过自定义`list_filter`来实现这个功能。具体步骤如下:
1. 定义一个自定义的Filter类,继承`django.contrib.admin.SimpleListFilter`类,重写`queryset`和`lookups`方法,来实现筛选功能。
```python
import datetime
class YearMonthFilter(admin.SimpleListFilter):
title = 'Year Month'
parameter_name = 'year_month'
def lookups(self, request, model_admin):
# 获取所有可以选择的年月列表
year_month_list = []
for obj in model_admin.model.objects.all():
year_month = obj.yearMonth.strftime('%Y-%m')
if year_month not in year_month_list:
year_month_list.append(year_month)
return [(ym, ym) for ym in year_month_list]
def queryset(self, request, queryset):
if self.value():
year_month = datetime.datetime.strptime(self.value(), '%Y-%m')
return queryset.filter(yearMonth=year_month)
```
2. 在`list_filter`中添加自定义的Filter类
```python
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', 'warehouse', YearMonthFilter)
```
这样就可以在右侧过滤器中选择任意年份和月份进行筛选了。
Django如何让视图函数只选择Article的is_active属性等于true的 class ArticleListView(ListView): model = Article template_name = '../templates/index/article_list.html' context_object_name = 'articles' ordering = ['-pub_date']
你可以重写 `get_queryset` 方法来过滤 `is_active=True` 的文章,如下所示:
```python
class ArticleListView(ListView):
model = Article
template_name = '../templates/index/article_list.html'
context_object_name = 'articles'
ordering = ['-pub_date']
def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(is_active=True)
```
这里我们调用了 `super().get_queryset()` 获取所有文章,然后使用 `filter(is_active=True)` 方法过滤出 `is_active=True` 的文章。这样就可以保证只有 `is_active=True` 的文章被返回给模板。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)