How to generate auto generate unique serial number by date and also It will be generate serial vise after delete in django


This Content is from Stack Overflow. Question asked by Shweta Shinde

  • I have to generate serial number like TES-0922-1,TES-0922-2 and so

  • here
    TES is (company_lable)
    0922 is (month and year)
    1,2 is (serial number)

  • I wish to generate unique number by date . If today is month 9 and year 22 and I am generating new data of this month then my serial number will be TES-0922-01.

  • Also If I have serial numbers TES-0922-01,TES-0922-02,TES-0922-03 and If I am deleting TES-0922-01 from them. afterwards when I creating new data my serial number will be TES-0922-04

  • Also If I have serial numbers TES-0922-01,TES-0922-02,TES-0922-03,TES-0922-04 and If I am deleting TES-0922-01, TES-0922-04 from them. afterwards when I creating new data my serial number will be TES-0922-04

Note : I am saving that data in the database and handling them by django queryset.

What I did is mentioned below but Its not working properly. Because I am counting my data by django queryset.

def sales_invoice(request):

current_time = 
latest_year = current_time.year

latest_month = current_time.month
month="%02d" % (latest_month)

company_label= Company_Setup.objects.get(id=request.session['company_id'])

if not existing_label:
    total_sales = Sales_Invoice.objects.filter(company=company_label).all().count() + 1


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?