3

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

5
  • 2
    If you uncomment 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 call self.type.save() (when you didn't even changed self.type - unless ` self.type` is supposed to be the same as what you get from VoucherTypeMaster.objects.get() but then what's the point when you already have a foreign key on VoucherTypeMaster ? Commented Mar 20, 2015 at 12:33
  • Did you consider model inheritance of Voucher to VoucherTypeMaster, that may make your code DRYer and less cumbersome.
    – djangonaut
    Commented Mar 20, 2015 at 12:40
  • also what happens when two requests happen at the same time? incrementing last_number isn't atomic?
    – dm03514
    Commented Mar 20, 2015 at 12:41
  • How will inheritance help in this case can u plz elaborate
    – user956424
    Commented Mar 21, 2015 at 15:05
  • stackoverflow.com/users/594589/dm03514 can you please suggest a better way to achieve this?Desperate
    – user956424
    Commented Mar 22, 2015 at 16:19

1 Answer 1

8

Overriding the save method on Voucher model and passing the VoucherTypeMaster and not its instance solved the problem: Increment the last_number, if the self.id is None

def save(self, *args, **kwargs):
    try:
        voucher_type = VoucherTypeMaster.objects.get(
            company=self.company,
            code=self.type.code
            )
        if self.id is None:
            voucher_type.last_number = voucher_type.last_number+1
            self.type = voucher_type
            voucher_type.save()
    except Exception,e:
        print e
    super(Voucher, self).save(*args, **kwargs)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.