Home > OS >  Increment Last Id from String in python Django
Increment Last Id from String in python Django

Time:07-16

I am trying to generate auto increment number as a ID with Company Label. Company Label will be changes for every user. So I cant use slicing here. My ID is like TES-PRODUCT-01

TES is company label PRODUCT is as it is name

But now I wish to change my last number when I am adding new product to TES-PRODUCT-02 and so on

Getting Error can only concatenate str (not "int") to str

Here is my code views.py

def add_new_stock(request):
stock_data=New_Stock_Entry.objects.all()
if not stock_data:
    latest_item_code="TES-PRODUCT-001"
else:
    latest_item_code = (New_Stock_Entry.objects.last()).item_code 1

get_tax_code=Tax_Settings.objects.values('name', 'tax_percentage','tax_id')

if request.method == 'POST':
    item = request.POST['item']
    hsn = request.POST['hsn']
    item_code=latest_item_code
    stock_in_date=request.POST['stock_in_date']
    quantity_in_hand=request.POST['quantity_in_hand']
    sales_price=request.POST['sales_price']
    item_description=request.POST['item_description']
    unit=request.POST['unit']
    tax_code = request.POST['tax_code']
    tax_obj = Tax_Settings.objects.get(tax_id=tax_code)
    item_creation_details = New_Stock_Entry.objects.create(item=item, hsn=hsn, item_code=item_code,stock_in_date=stock_in_date,quantity_in_hand=quantity_in_hand,sales_price=sales_price ,item_description=item_description, unit=unit, tax_code=tax_obj)
    item_creation_details.save()
    print("item_details",item_creation_details)
    return render(request,"inventory/add-stock.html")
return render(request,"inventory/add-stock.html",{'get_tax':get_tax_code,'latest_item_code':latest_item_code})

html

<input type="text"  placeholder="TES-PRODUCT-{{latest_item_code}}" name="item_code">

How Can I increment my last number from string?

CodePudding user response:

You can simply use "f-strings"

total_stock = New_Stock_Entry.objects.all().count()   1
latest_item_code=f"TES-PRODUCT-{total_stock}"
  • Related