[Fixed]-Count and sort according to column value in django query

1👍

Report_Shope.objects.values('shop_name').annotate(
   shop_count=Count('id')
).order_by('shop_count')

Aggregation docs.

👤avd

0👍

You can use count()

Report_Shop.objects.filter(barcode='value').count()

and to sort you can add meta in your model to default order

class Report_Shop(models.Model):
    barcode = models.CharField(max_length=500)
    email = models.CharField(max_length=500)
    shop_name = models.CharField(max_length=500)

    class Meta:
        ordering = ('-barcode',) 

or in your queryset with orfer_by()

Report_Shop.objects.order_by('barcode')
Report_Shop.objects.order_by('-barcode')

The negative sign in front of “-barcode” indicates descending order. Ascending order is implied. To order randomly, use “?”.

Leave a comment