Re: Negative Stock Prevention

2022-09-01 Thread Ryan Nowakowski
If you get this error during migration you likely have some existing Stock rows 
in your database with quantity that is less than 0. If this site is in 
production already, you'll need to create a data migration to change your 
negative quantities to zero.

https://docs.djangoproject.com/en/4.1/topics/migrations/#data-migrations

If this project isn't in production yet, just delete your database and 
migrations and start from ground zero.

On September 1, 2022 12:18:22 AM CDT, tech george  wrote:
>Hello,
>
>Sorry the error is:
>
>django.db.utils.IntegrityError: CHECK constraint failed: quantity
>
>
>
>On Thu, Sep 1, 2022 at 3:45 AM Ryan Nowakowski  wrote:
>
>> I don't see any error. Did you forget to post it?
>>
>> On August 31, 2022 5:57:32 AM CDT, tech george 
>> wrote:
>>>
>>> Hello,
>>>
>>> Sorry for the late reply.
>>>
>>> I changed the models as below and added checkConstraint , But when I
>>> migrate I get the below error.
>>>
>>> What am I still doing wrong?
>>>
>>> class Stock(models.Model):
>>> quantity = models.PositiveIntegerField(default='0', blank=True,
>>> null=True)
>>> reorder_level = models.PositiveIntegerField(default='0', blank=True,
>>> null=True)
>>>
>>> class Meta:
>>> CheckConstraint(check=Q(quantity__gt=0), name='quantity')
>>>
>>>
>>>
>>> On Tue, Aug 30, 2022 at 6:09 PM Thomas Couch  wrote:
>>>
 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+unsubscr...@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
 
 .

>>> --
>> 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+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/DDF83E16-47C4-4E38-90DE-6CDAE28268DB%40fattuba.com
>> 
>> .
>>
>
>-- 
>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+unsubscr...@googlegroups.com.
>To view this discussion on the web visit 
>https://groups.google.com/d/msgid/django-users/CADYG20H6Yv_Y0vkgqVzOU90emRP%3DnvUw7vRrM--HGNbb0zZCAA%40mail.gmail.com.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5FF4241B-B0E2-4082-9537-F1CDE9AF821D%40fattuba.com.


Re: Negative Stock Prevention

2022-08-31 Thread tech george
I managed to remove the error,

I had a negative stock in my DB.

Thanks a lot for the help.

On that note, I was also trying to get the dispensed from stock

I was trying to do this but I don't think it can work since there is
already a foreign key at Dispense model fetching drug_id from stock.

What's the best way out?

@property
def dispensed(self):
return (self.quantity - self.dispense.quantity)




On Thu, Sep 1, 2022 at 6:09 AM Ammar Mohammed  wrote:

> I don't think you need that constraint after using the
> PositiveIntegerField .
>
>
> On Thu, 1 Sep 2022, 02:45 Ryan Nowakowski,  wrote:
>
>> I don't see any error. Did you forget to post it?
>>
>> On August 31, 2022 5:57:32 AM CDT, tech george 
>> wrote:
>>>
>>> Hello,
>>>
>>> Sorry for the late reply.
>>>
>>> I changed the models as below and added checkConstraint , But when I
>>> migrate I get the below error.
>>>
>>> What am I still doing wrong?
>>>
>>> class Stock(models.Model):
>>> quantity = models.PositiveIntegerField(default='0', blank=True,
>>> null=True)
>>> reorder_level = models.PositiveIntegerField(default='0', blank=True,
>>> null=True)
>>>
>>> class Meta:
>>> CheckConstraint(check=Q(quantity__gt=0), name='quantity')
>>>
>>>
>>>
>>> On Tue, Aug 30, 2022 at 6:09 PM Thomas Couch  wrote:
>>>
 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+unsubscr...@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
 
 .

>>> --
>> 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+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/DDF83E16-47C4-4E38-90DE-6CDAE28268DB%40fattuba.com
>> 
>> .
>>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAHs1H7uN4xCF%2B7AKqxSufatWiUN_n9cw4uBALo-ENFOfirp_hg%40mail.gmail.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADYG20EXeqBXyAQsG0zpk6HXxcKixhjvy_R9z1QiY6%3Da5cAvFg%40mail.gmail.com.


Re: Negative Stock Prevention

2022-08-31 Thread tech george
Hello,

Sorry the error is:

django.db.utils.IntegrityError: CHECK constraint failed: quantity



On Thu, Sep 1, 2022 at 3:45 AM Ryan Nowakowski  wrote:

> I don't see any error. Did you forget to post it?
>
> On August 31, 2022 5:57:32 AM CDT, tech george 
> wrote:
>>
>> Hello,
>>
>> Sorry for the late reply.
>>
>> I changed the models as below and added checkConstraint , But when I
>> migrate I get the below error.
>>
>> What am I still doing wrong?
>>
>> class Stock(models.Model):
>> quantity = models.PositiveIntegerField(default='0', blank=True,
>> null=True)
>> reorder_level = models.PositiveIntegerField(default='0', blank=True,
>> null=True)
>>
>> class Meta:
>> CheckConstraint(check=Q(quantity__gt=0), name='quantity')
>>
>>
>>
>> On Tue, Aug 30, 2022 at 6:09 PM Thomas Couch  wrote:
>>
>>> 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+unsubscr...@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
>>> 
>>> .
>>>
>> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/DDF83E16-47C4-4E38-90DE-6CDAE28268DB%40fattuba.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADYG20H6Yv_Y0vkgqVzOU90emRP%3DnvUw7vRrM--HGNbb0zZCAA%40mail.gmail.com.


Re: Negative Stock Prevention

2022-08-31 Thread Ammar Mohammed
I don't think you need that constraint after using the PositiveIntegerField
.


On Thu, 1 Sep 2022, 02:45 Ryan Nowakowski,  wrote:

> I don't see any error. Did you forget to post it?
>
> On August 31, 2022 5:57:32 AM CDT, tech george 
> wrote:
>>
>> Hello,
>>
>> Sorry for the late reply.
>>
>> I changed the models as below and added checkConstraint , But when I
>> migrate I get the below error.
>>
>> What am I still doing wrong?
>>
>> class Stock(models.Model):
>> quantity = models.PositiveIntegerField(default='0', blank=True,
>> null=True)
>> reorder_level = models.PositiveIntegerField(default='0', blank=True,
>> null=True)
>>
>> class Meta:
>> CheckConstraint(check=Q(quantity__gt=0), name='quantity')
>>
>>
>>
>> On Tue, Aug 30, 2022 at 6:09 PM Thomas Couch  wrote:
>>
>>> 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+unsubscr...@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
>>> 
>>> .
>>>
>> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/DDF83E16-47C4-4E38-90DE-6CDAE28268DB%40fattuba.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHs1H7uN4xCF%2B7AKqxSufatWiUN_n9cw4uBALo-ENFOfirp_hg%40mail.gmail.com.


Re: Negative Stock Prevention

2022-08-31 Thread Ryan Nowakowski
I don't see any error. Did you forget to post it?

On August 31, 2022 5:57:32 AM CDT, tech george  wrote:
>Hello,
>
>Sorry for the late reply.
>
>I changed the models as below and added checkConstraint , But when I
>migrate I get the below error.
>
>What am I still doing wrong?
>
>class Stock(models.Model):
>quantity = models.PositiveIntegerField(default='0', blank=True,
>null=True)
>reorder_level = models.PositiveIntegerField(default='0', blank=True,
>null=True)
>
>class Meta:
>CheckConstraint(check=Q(quantity__gt=0), name='quantity')
>
>
>
>On Tue, Aug 30, 2022 at 6:09 PM Thomas Couch  wrote:
>
>> 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+unsubscr...@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
>> 
>> .
>>
>
>-- 
>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+unsubscr...@googlegroups.com.
>To view this discussion on the web visit 
>https://groups.google.com/d/msgid/django-users/CADYG20F0occUAajR%3DSseRtiW%2Bzjh4THmgN6FEFbFsGcpsmMLZg%40mail.gmail.com.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/DDF83E16-47C4-4E38-90DE-6CDAE28268DB%40fattuba.com.


Re: Negative Stock Prevention

2022-08-31 Thread tech george
Hello,

Sorry for the late reply.

I changed the models as below and added checkConstraint , But when I
migrate I get the below error.

What am I still doing wrong?

class Stock(models.Model):
quantity = models.PositiveIntegerField(default='0', blank=True,
null=True)
reorder_level = models.PositiveIntegerField(default='0', blank=True,
null=True)

class Meta:
CheckConstraint(check=Q(quantity__gt=0), name='quantity')



On Tue, Aug 30, 2022 at 6:09 PM Thomas Couch  wrote:

> 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+unsubscr...@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
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADYG20F0occUAajR%3DSseRtiW%2Bzjh4THmgN6FEFbFsGcpsmMLZg%40mail.gmail.com.


Re: Negative Stock Prevention

2022-08-30 Thread Thomas Couch
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+unsubscr...@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.


Re: Negative Stock Prevention

2022-08-30 Thread Ryan Nowakowski
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/20220830144358.GE1858%40fattuba.com.


Re: Negative Stock Prevention

2022-08-30 Thread Derek
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+unsubscr...@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.


Negative Stock Prevention

2022-08-29 Thread tech george
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADYG20GtQH-g%2Br2-T7VaD3xna-jtydr%3DsH6RSQnBJfBza39caw%40mail.gmail.com.