Im confused in counting the items that are quantities are below reorder level. The reorder level is a column in the model of an item too, i think i need to compare their values in __lte in filters but it's not working.
Models.py
class Item(models.Model):
ItemName = models.CharField(max_length=255, blank=True, null=True)
Quantity = models.IntegerField(null=True, default=1,
validators=[
MaxValueValidator(100),
MinValueValidator(0)
])
ModelNum = models.CharField(max_length=255, blank=True, null=True)
Category = models.ForeignKey(Category,on_delete=models.CASCADE, null=True)
date_created = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
is_draft = models.BooleanField(default=True)
reorder_level = models.IntegerField(blank=True, default=10,
validators=[
MaxValueValidator(100),
MinValueValidator(1)
])
class Meta:
verbose_name_plural = 'Item'
def __str__(self):
return f'{self.ItemName}'
Views.py
def dashboard_inventory(request):
items_count = Item.objects.all().count()
reorder_count = Item.objects.filter(Quantity_lte=reorder_level).count()
context={
'items_count' : items_count,
}
template_name ='inventory-admin/inventory-dashboard.html'
return render(request, template_name, context)
CodePudding user response:
You can use F expressions
to compare those two columns like this:
from django.db.models import F
Item.objects.filter(Quantity__lte=F('reorder_level')).count()
This will get all Item
s with Quantity
less than or equal to its own reorder_level