自动发票在django后端发票新的发票

编程入门 行业动态 更新时间:2024-10-27 22:21:02
本文介绍了自动发票在django后端发票新的发票的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想自动发送3位数字字符和4位数的发票号码。

类发票: invoice_no = models.CharField(max_length = 500,null = True,blank = True,validators = [RegexValidator(regex ='^ [a-zA-Z0-9] * $',message ='发票必须为字母数字' ,code ='invalid_invoice number'),])

我在后端注册这个模型。但现在当我点击管理员的创建发票时,发票应该是自动填充的。当我再次点击管理员中的新发票时,发票号应增加1,应为自动字段。

发票编号MAG0001,MAG0002,MAG0003等,当我点击创建新发票时,这应该是admin中的auto field。

解决方案

定义一个函数来生成发票号码。 $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ )如果不是last_invoice:返回'MAG0001' invoice_no = last_invoice.invoice_no invoice_int = int(invoice_no.split('MAG')[ - 1])$ ​​b $ b new_invoice_int = invoice_int + 1 new_invoice_no ='MAG'+ str(new_invoice_int)返回new_invoice_no

现在使用此函数作为模型库中的默认值。

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

这只是一个想法。修改功能以匹配您首选的发票编号格式。

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=True, blank=True, validators=[RegexValidator(regex='^[a-zA-Z0-9]*$',message='Invoice must be Alphanumeric',code='invalid_invoice number'),])

I register this model in backend. But now when i click on create invoice in admin the invoice should be auto filled. When i again click on create new invoice in admin, the invoice_number should be incremented by one and should be auto field.

Ex for Invoice number MAG0001, MAG0002, MAG0003 etc and this should be auto field in admin when i click on create new invoice.

解决方案

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.

更多推荐

自动发票在django后端发票新的发票

本文发布于:2023-10-30 12:30:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发票   后端   django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!