Auto increament the invoice number in django backend for new invoice

后端 未结 4 1734
失恋的感觉
失恋的感觉 2021-02-09 14:25

I want to auto increament the invoice number which is 3 digits char and 4 digits number.

class Invoice:
    invoice_no = models.CharField(max_length=500, null=Tr         


        
相关标签:
4条回答
  • 2021-02-09 15:05

    Define a function to generate invoice number.

    def increment_invoice_number():
        last_invoice = Invoice.objects.all().order_by('id').last()
        if not last_invoice:
             return 'MAG0001'
        invoice_no = last_invoice.invoice_no
        invoice_int = int(invoice_no.split('MAG')[-1])
        new_invoice_int = invoice_int + 1
        new_invoice_no = 'MAG' + str(new_invoice_int)
        return new_invoice_no
    

    Now use this function as default value in your model filed.

    invoice_no = models.CharField(max_length=500, default=increment_invoice_number, null=True, blank=True)
    

    This is just an idea. Modify the function to match your preferred invoice number format.

    0 讨论(0)
  • 2021-02-09 15:05
    def invoiceIncrement():
        get_last_invoice_number
        incremente_last_invoice_number
        return next_invoice_number
    
    class Invoice:
        invoice_no = models.CharField(max_length=500, null=True, blank=True, 
            validators=[RegexValidator(regex='^[a-zA-Z0-9]*$',
            message='Invoice must be Alphanumeric',code='invalid_invoice number'),], 
            default=invoiceIncrement)
    

    Try this: there are some obvious issues:

    1. if more than one person adds an invoice at the same time, could have collision

    2. will need to make an extra db call each time you create a new invoice.

    Also: you may want to just consider using either an auto_increment or UUID.

    0 讨论(0)
  • 2021-02-09 15:10

    Maybe this code can help

    def increment_invoice_number():
        last_invoice = Invoice.objects.all().order_by('id').last()
        if not last_invoice:
            return 'MAG0001'
        invoice_no = last_invoice.invoice_no
        new_invoice_no = str(int(invoice_no[4:]) + 1)
        new_invoice_no = invoice_no[0:-(len(new_invoice_no))] + new_invoice_no
        return new_invoice_no
    
    0 讨论(0)
  • 2021-02-09 15:16

    In above arulmr answer just edit char field

    def increment_invoice_number():
        last_invoice = Invoice.objects.all().order_by('id').last()
        if not last_invoice:
            return 'MAG0001'
        invoice_no = last_invoice.invoice_no
        invoice_int = int(invoice_no.split('MAG')[-1])
        width = 4
        new_invoice_int = invoice_int + 1
        formatted = (width - len(str(new_invoice_int))) * "0" + str(new_invoice_int)
        new_invoice_no = 'MAG' + str(formatted)
        return new_invoice_no  
    
    class Invoice(models.Model):
        invoice_no = models.CharField(max_length = 500, default = increment_invoice_number, null = True, blank = True)
    

    This will work fine.

    0 讨论(0)
提交回复
热议问题