Auto increament the invoice number in django backend for new invoice

后端 未结 4 1733
失恋的感觉
失恋的感觉 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条回答
  •  闹比i
    闹比i (楼主)
    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.

提交回复
热议问题