I have a master model which creates alphanumeric automatically for different types of Vouchers for different companies. How do I update the master model. The models:
class VoucherTypeMaster(models.Model):
code = models.CharField(max_length=12,null=True,blank=True)
description = models.CharField(max_length=30,null=True,blank=True)
last_number = models.IntegerField(null=True,blank=True)
company = models.ForeignKey(Company,
related_name='voucher_master_company')
class Meta:
unique_together = ('code','company')
class Voucher(models.Model):
type = models.ForeignKey(VoucherTypeMaster)
date = models.DateField(default=datetime.datetime.now().date())
company = models.ForeignKey(Company,
related_name='voucher_company')
number = models.CharField(max_length=20,null=True,blank=True)
narration = models.CharField(max_length=30,null=True,blank=True)
amount = models.DecimalField(decimal_places=2,max_digits=9)
# class Meta:
# unique_together = ('company','number','date')
def __unicode__(self):
return '%s - %s' %(self.number,self.narration)
def save(self, *args, **kwargs):
try:
voucher_type = VoucherTypeMaster.objects.get(
company=self.company,
code=self.type.code
)
voucher_type.last_number += 1
voucher_type.save()
self.number = voucher_type.last_number
# self.type.save() # throws exception
except Exception,e:
print e
super(Voucher, self).save(*args, **kwargs)
If I uncomment self.type.save() Traceback got:
Manager isn't accessible via VoucherTypeMaster instances
How to update the VoucherTypeMaster model with the next value? Using django 1.6.5, linux
self.type.save()
, what you get is not a traceback but just the exception message. Do yourself (and the world) a favour and get rid of that harmful useless except clause - it only prevents you from getting relevant informations about your exception... Now for your error: why do you want to callself.type.save()
(when you didn't even changedself.type
- unless ` self.type` is supposed to be the same as what you get fromVoucherTypeMaster.objects.get()
but then what's the point when you already have a foreign key onVoucherTypeMaster
?