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', 'month', 'warehouse', 'subjectMatter'] list_filter = ('name', 'month', 'warehouse',) def apply_prove(self, obj): return format_html("<span>认证完成</span>") apply_prove.short_description = '入库次数' apply_prove.allow_tags = True def get_queryset(self, request): qs = super().get_queryset(request) if 'name' in request.GET and request.GET['name'] != 'none': # 条件查询 qs = qs.filter(name=request.GET['name']) if 'subjectMatter' in self.list_display: self.list_display.remove('subjectMatter') # qs = qs.values('month', 'warehouse').annotate(apply_prove=Count('id')).distinct() # self.list_display = ['name', 'month', 'warehouse', 'apply_prove'] if 'apply_prove' not in self.list_display: self.list_display.append('apply_prove') else: if 'subjectMatter' not in self.list_display: self.list_display.append('subjectMatter') if "apply_prove" in self.list_display: self.list_display.remove('apply_prove') return qs 代码中的qs = qs.filter(name=request.GET['name']),当前qs查询出来5个QuerySet实例,如何根据month和warehouse去重,并且将重复的条数定义为新字段添加在数据中,去重后结果不是字典,而是跟qs = qs.filter(name=request.GET['name'])结果类型一致
时间: 2024-01-21 21:03:07 浏览: 25
可以使用`values`和`annotate`方法来实现按照`month`和`warehouse`字段去重,并且将重复的条数定义为新字段添加在数据中。
可以将以下代码:
```
# qs = qs.filter(name=request.GET['name']).distinct()
```
替换为以下代码:
```
qs = qs.filter(name=request.GET['name']).values('month', 'warehouse').annotate(apply_prove=Count('id')).distinct()
```
这样就可以按照`month`和`warehouse`字段去重,并且将重复的条数定义为新字段`apply_prove`添加在数据中。注意要引入`Count`函数。
相关问题
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)
```
这样就可以在右侧过滤器中选择任意年份和月份进行筛选了。
我有一个渠道的点云文件为ditch.pcd,我想要提取渠道的中轴线,并获得渠道的断面切片。c++代码是什么
提取渠道的中轴线以及获得渠道的断面切片是点云处理中的经典问题。下面是一个简单的C++代码示例,使用PCL库实现了该功能。
```c++
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/features/normal_3d.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/passthrough.h>
#include <pcl/sample_consensus/ransac.h>
#include <pcl/sample_consensus/sac_model_line.h>
#include <pcl/sample_consensus/sac_model_plane.h>
#include <pcl/sample_consensus/sac_model_circle3d.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>
#include <vector>
int main (int argc, char** argv)
{
// Load input file
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ> ("ditch.pcd", *cloud) == -1) //* load the file
{
PCL_ERROR ("Couldn't read file ditch.pcd \n");
return (-1);
}
// Downsample input cloud
pcl::VoxelGrid<pcl::PointXYZ> sor;
sor.setInputCloud (cloud);
sor.setLeafSize (0.1f, 0.1f, 0.1f);
sor.filter (*cloud);
// Extract points within a certain range
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud (cloud);
pass.setFilterFieldName ("z");
pass.setFilterLimits (-1.0, 1.0);
pass.filter (*cloud);
// Estimate normals
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
ne.setInputCloud (cloud);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
ne.setSearchMethod (tree);
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);
ne.setRadiusSearch (0.05);
ne.compute (*cloud_normals);
// Create the segmentation object for the planar model and set all the parameters
pcl::SACSegmentationFromNormals<pcl::PointXYZ, pcl::Normal> seg;
seg.setOptimizeCoefficients (true);
seg.setModelType (pcl::SACMODEL_NORMAL_PLANE);
seg.setNormalDistanceWeight (0.1);
seg.setMethodType (pcl::SAC_RANSAC);
seg.setMaxIterations (100);
seg.setDistanceThreshold (0.03);
seg.setInputCloud (cloud);
seg.setInputNormals (cloud_normals);
// Obtain the plane inliers and coefficients
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
seg.segment (*inliers, *coefficients);
// Extract the planar inliers from the input cloud
pcl::ExtractIndices<pcl::PointXYZ> extract;
extract.setInputCloud (cloud);
extract.setIndices (inliers);
extract.setNegative (true);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
extract.filter (*cloud_filtered);
// Create the segmentation object for the line model and set all the parameters
pcl::SACSegmentation<pcl::PointXYZ> seg_line;
seg_line.setOptimizeCoefficients (true);
seg_line.setModelType (pcl::SACMODEL_LINE);
seg_line.setMethodType (pcl::SAC_RANSAC);
seg_line.setMaxIterations (1000);
seg_line.setDistanceThreshold (0.1);
seg_line.setInputCloud (cloud_filtered);
// Obtain the line inliers and coefficients
pcl::ModelCoefficients::Ptr coefficients_line (new pcl::ModelCoefficients);
pcl::PointIndices::Ptr inliers_line (new pcl::PointIndices);
seg_line.segment (*inliers_line, *coefficients_line);
// Extract the line inliers from the input cloud
extract.setInputCloud (cloud_filtered);
extract.setIndices (inliers_line);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_line (new pcl::PointCloud<pcl::PointXYZ>);
extract.filter (*cloud_line);
// Create a PCL visualizer
pcl::visualization::PCLVisualizer viewer ("PCL Viewer");
// Add input cloud to viewer
viewer.addPointCloud<pcl::PointXYZ> (cloud, "cloud");
// Add line fit to viewer
viewer.addLine (*coefficients_line, 0, 255, 0, "line");
// Add plane fit to viewer
viewer.addPlane (*coefficients, "plane");
// Display viewer
while (!viewer.wasStopped ())
{
viewer.spinOnce ();
}
return (0);
}
```
这段代码使用了点云库PCL(Point Cloud Library),实现了以下步骤:
1. 从文件中读取点云数据。
2. 对点云进行下采样,减少计算量。
3. 选取某个范围内的点云,去除无关的噪声。
4. 估计法向量,用于后续的拟合。
5. 使用RANSAC算法拟合平面模型,去除地面点云。
6. 使用RANSAC算法拟合直线模型,提取渠道的中轴线。
7. 可视化结果,包括原始点云、平面模型和直线模型。
你可以根据自己的需求进行修改和扩展。例如,添加断面切片的方法可以是:
1. 将中轴线上的点按照顺序连接起来,形成多边形。
2. 在多边形两侧分别选取一定数量的点云,按照垂线方向切割出一系列的断面。
3. 对每个断面进行分析和处理,例如计算面积、周长、高度等。