#This is my inventory model. I want to get the inventory items that have the lowest quantity in the model.
JavaScript
x
12
12
1
class Inventory(models.Model):
2
name = models.CharField(max_length=100)
3
purchase_date = models.DateTimeField()
4
category = models.ForeignKey(Category, on_delete=models.CASCADE)
5
6
quantity = models.CharField(max_length=50)
7
purchase_price = models.FloatField(max_length=50)
8
selling_price = models.FloatField(max_length=50)
9
description = models.CharField(max_length=100)
10
location = models.ForeignKey(Locations, on_delete=models.CASCADE)
11
created_date = models.DateField(auto_now_add=True)
12
Advertisement
Answer
To get a number of results with the lowest or highest value you first order by that field order_by('quantity')
so that the results you want will be first, then you can slice the queryset to limit the number of results
The quantity
field should really be a PositiveIntegerField
or IntegerField
since it stores integers
JavaScript
1
2
1
top_five_least_quantity = Inventory.objects.order_by('quantity')[:5]
2