0

I am trying to generate an auto-increment number as an ID with Company Label. Company Labels will be changing for every user. So I cant use slicing here.

My ID is like TES-PRODUCT-01

TES is a 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" class="form-control" placeholder="TES-PRODUCT-{{latest_item_code}}" name="item_code">

How Can I increment my last number from string?

1

1 Answer 1

0

You can simply use "f-strings"

total_stock = New_Stock_Entry.objects.all().count() + 1
latest_item_code=f"TES-PRODUCT-{total_stock}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.