#This is my inventory model. I want to get the inventory items that have the lowest quantity in the model.
class Inventory(models.Model):
name = models.CharField(max_length=100)
purchase_date = models.DateTimeField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
quantity = models.CharField(max_length=50)
purchase_price = models.FloatField(max_length=50)
selling_price = models.FloatField(max_length=50)
description = models.CharField(max_length=100)
location = models.ForeignKey(Locations, on_delete=models.CASCADE)
created_date = models.DateField(auto_now_add=True)
CodePudding user response:
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
top_five_least_quantity = Inventory.objects.order_by('quantity')[:5]