简体   繁体   English

当数量低于 django 中的再订购水平时如何计算项目

[英]How to count items when their quantity is below reorder level in django

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.重新排序级别也是一个项目的 model 中的一列,我想我需要在过滤器中比较它们在 __lte 中的值,但它不起作用。

Models.py模型.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视图.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)

You can use F expressions to compare those two columns like this:您可以使用F expressions来比较这两列,如下所示:

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这将得到所有ItemQuantity小于或等于它自己的reorder_level

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM