Tuesday, August 30, 2022

Re: Negative Stock Prevention

I don't see where you define the quantity variable, should that be instance.quantity? Also, presumably you want to check if quantity is greater than or equal to qu rather than 0.
Try changing `if quantity > 0` to `if instance.quantity >= qu`

On Tuesday, August 30, 2022 at 3:44:51 PM UTC+1 Ryan Nowakowski wrote:
On Mon, Aug 29, 2022 at 05:18:39PM +0300, tech george wrote:
> Please help crack the below code, I want to prevent negative stock, and if
> the stock is == 0, deduct it from reorder_level instead.
> Currently, the stock goes negative.
> models.py
> class Stock(models.Model):
> quantity = models.IntegerField(default='0', blank=True, null=True)
> reorder_level = models.IntegerField(default='0', blank=True, null=True)
> class Dispense(models.Model):
> drug_id = models.ForeignKey(Stock,
> on_delete=models.SET_NULL,null=True,blank=False)
> dispense_quantity = models.PositiveIntegerField(default='1',
> blank=False, null=True)
> taken=models.CharField(max_length=300,null=True, blank=True)

Maybe change quantity and reorder_level to PositiveIntegerField?

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9ef2d260-7c1a-4ff1-95ca-c13ded5f9f7bn%40googlegroups.com.

No comments:

Post a Comment