Python 在_related()查询集上分组

Python 在_related()查询集上分组,python,django,group-by,Python,Django,Group By,我已经看过了,但我似乎无法让它发挥作用 我有一个queryset,它带来了4个相关的表。我想从“顶表”返回值并将它们分组 要分组的列有:区域东距(E)、区域北距(N)、上下文编号(C)、样本编号(S) 当前数据输出示例 E |N |C |S |material 99|526|101|1 ... |seed 99|526|101|1 ... |grain 99|526|101|1 ... |pip 99|526|101|2 ... |seed 99|526|101|2 ... |gra

我已经看过了,但我似乎无法让它发挥作用

我有一个queryset,它带来了4个相关的表。我想从“顶表”返回值并将它们分组

要分组的列有:区域东距(E)、区域北距(N)、上下文编号(C)、样本编号(S)

当前数据输出示例

E |N  |C  |S     |material
99|526|101|1 ... |seed
99|526|101|1 ... |grain
99|526|101|1 ... |pip
99|526|101|2 ... |seed
99|526|101|2 ... |grain
99|526|101|2 ... |pip
我只想返回E | N | C | S一次,但仍然能够列出与之相关的每种材料。无需创建另一个def即可实现此功能吗

#views.py
def botany(request):
  botany = FractionMaterialsPresent.objects.select_related()
  template = 'botany/test.html'
  return render(request, template, {'botany': botany})

#models.py
class Botany(models.Model):
  botany_id = models.AutoField(primary_key=True)
  sample_id = models.IntegerField(blank=True, null=True)
  area_easting = models.IntegerField(blank=True, null=True)
  area_northing = models.IntegerField(blank=True, null=True)
  context_number = models.IntegerField(blank=True, null=True)
  sample_number = models.IntegerField(blank=True, null=True)
  entry_date = models.DateTimeField(auto_now_add=True)
  analysis_date = models.DateTimeField(auto_now=True)
  analyst = models.CharField(max_length=200, default='')
  notes = models.CharField(max_length=600, default='')

class Fraction(models.Model):
  fraction_id = models.AutoField(primary_key=True)
  botany_id = models.ForeignKey(Botany, db_column='botany_id', on_delete = models.PROTECT)
  proportion_analysed = models.DecimalField(max_digits=5, decimal_places=3)
  soil_volume = models.DecimalField(max_digits=15, decimal_places=4)
  sample_volume = models.DecimalField(max_digits=15, decimal_places=4)
  sample_weight = models.DecimalField(max_digits=15, decimal_places=4)

class FractionMaterialsPresent(models.Model):
  material_id = models.AutoField(primary_key=True)
  fraction_id = models.ForeignKey(Fraction, db_column='fraction_id', on_delete = models.PROTECT)
  material = models.CharField(max_length=200, default='')

可能的重复我承认,在我原来的帖子中,我说我无法应用它,我认为第二次在这里声明它没有什么意义。