Tuesday, August 30, 2022

Re: Negative Stock Prevention

As a start, the logic checks for the model should not be in views.py but rather with the model object (in models.py).  You can extend the save() method, for example, and add the checks there.

See: 
* https://docs.djangoproject.com/en/4.1/ref/models/instances/#saving-objects
* https://docs.djangoproject.com/en/4.1/ref/models/instances/#what-happens-when-you-save
* https://docs.djangoproject.com/en/4.1/topics/db/models/#overriding-model-methods

On Monday, 29 August 2022 at 16:19:28 UTC+2 techg...@gmail.com wrote:
Hello,

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)

views.py
try:  

if request.method == 'POST':
if form.is_valid():
username = form.cleaned_data['taken']
qu=form.cleaned_data['dispense_quantity']
ka=form.cleaned_data['drug_id']
# print(username)



stock= eo=Stock.objects.annotate(
expired=ExpressionWrapper(Q(valid_to__lt=Now()), output_field=BooleanField())
).filter(expired=False).get(id=username)
form=DispenseForm(request.POST or None, instance=stock)
instance=form.save()
# print(instance)
if quantity > 0
instance.quantity-=qu
instance.save()
else:
instance.reorder_level-=qu
instanc.save()

form=DispenseForm(request.POST or None ,initial={'patient_id':queryset})
form.save()

messages.success(request, "Drug Has been Successfully Dispensed")

return redirect('manage_patient_pharmacist')
else:
messages.error(request, "Validity Error")

return redirect('manage_patient_pharmacist')

context={
"patients":queryset,
"form":form,
# "stocks":stock,
"drugs":drugs,
"prescrips":prescrips,
"expired":ex,
"expa":eo,

}
if request.method == 'POST':

print(drugs)
context={
"drugs":drugs,
form:form,
"prescrips":prescrips,
"patients":queryset,
"expired":ex,
"expa":eo,

}
except:
messages.error(request, "Dispensing Not Allowed! The Drug is Expired ,please do a re-stock ")
return redirect('manage_patient_pharmacist')
context={
"patients":queryset,
"form":form,
# "stocks":stock,
"drugs":drugs,
"prescrips":prescrips,
"expired":ex,
"expa":eo,

}

return render(request,'templates/discharge.html',context)

--
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/e7001283-8260-44c3-974b-03ec4ef56ed3n%40googlegroups.com.

No comments:

Post a Comment