I've look at this How to query as GROUP BY in django? but I can't seem to get it to work.
I have a queryset which brings in 4 related tables. I would like to return values from the 'top table' and group them together.
Columns to group on are: area_easting(E), area_northing(N), context_number(C), sample_number(S)
example current data output
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
I only want to return the E|N|C|S once but still be able to list each material associated with it. Is this possible without having to create another 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='')