hello if i had model like this
class Customer(models.Model):
name = models.CharField(max_length=200,null= True)
phone = models.CharField(max_length=200,null= True)
email = models.CharField(max_length=200,null= True)
date_created = models.DateTimeField(auto_now_add= True,null = True)
def __str__(self):
return self.name
class Tag(models.Model):
name = models.CharField(max_length=200,null= True)
def __str__(self):
return self.name
class Item(models.Model):
CATEGORY = (
('Gudang Kering', 'Gudang Kering'),
('Gudang Basah','Gudang Basah'),
)
name = models.CharField(max_length=200,null= True)
stock = models.IntegerField(default='0', blank=False, null=True)
category = models.CharField(max_length=200,null= True,choices=CATEGORY)
reorderlevel = models.IntegerField(default='0', blank=False, null=True)
maxreorderlevel = models.IntegerField(default='0', blank=False, null=True)
description = models.CharField(max_length=200,null= True, blank= True)
date_created = models.DateTimeField(auto_now_add= True)
tags = models.ManyToManyField(Tag)
def __str__(self):
return self.name
class Issue(models.Model):
STATUS = (
('Pending', 'Pending'),
('Granted','Granted'),
('Denied','Denied'),
)
customer = models.ForeignKey(Customer, null=True, on_delete= models.SET_NULL)
item = models.ForeignKey(Item, null=True, on_delete= models.SET_NULL)
quantity = models.IntegerField(default='0', blank=False, null=True)
date_created = models.DateTimeField(auto_now_add=True, auto_now=False)
status = models.CharField(max_length=200,null= True, choices=STATUS)
def __str__(self):
return str(self.quantity)+' '+str(self.item)+' '+self.status + ' ' +str(self.customer)
#print(def__str__(self))
class Receive(models.Model):
STATUS = (
('Pending', 'Pending'),
('Granted','Granted'),
('Denied','Denied'),
)
customer = models.ForeignKey(Customer, null=True, on_delete= models.SET_NULL)
item = models.ForeignKey(Item, null=True, on_delete= models.SET_NULL)
quantity = models.IntegerField(default='0', blank=False, null=True)
date_created = models.DateTimeField(auto_now_add=True, auto_now=False)
status = models.CharField(max_length=200,null= True, choices=STATUS)
def __str__(self):
return str(self.quantity)+' '+str(self.item)+' '+self.status + ' ' +str(self.customer)
def updateIssue(request, pk):
issue = Issue.objects.get(id=pk)
item = Item.objects.all()
form = UpdateIssueForm(instance=issue)
if request.method == 'POST':
form = UpdateIssueForm(request.POST,instance=issue)
#print ('printing:',request.POST)
if form.is_valid():
instance = form.save(commit=False)
if instance.status == 'Granted':
item.stock -= instance.quantity
instance.save()
else:
instance.save()
return redirect('/')
in this case i want to make it so data from model item.stock which is an int will be reduced by
instance.quantity which is an int data from model Issue if status ==“Granted”
What I Tried
I did this succesfully if the model is only 1(stock and quantity_issue in same model)
using instance.stock -= instance.quantity
How do i make this work if it is from 2 different models?
Some Ideas
- I read that i can solve this by using properthy method but i have never used it much less with an if else condition
- I can get it to update the quantity if it is in the same model instance,if i can somehow get an instance2 the surely i can do a simple plus or minus after save right?