setting a value for form field after POST

Hi,
I want to change the value of a field (sequence) for form after posting. In fact I used

order_form.initial['sequence'] = int(Order.objects.order_by('-id')[0].sequence) + 1

, but it does not change the seuqence field after posting the request.

def add_order_regular_customer(request):
    if request.method == 'POST':
        source = UserProfileInfo.objects.get(user=request.POST['source'])
        product = Product.objects.get(id=request.POST['product'])
        current_inventory = Movement.objects.filter(destination=source).filter(product=product).aggregate(Sum('remain_quantity'))['remain_quantity__sum']
        quantity = request.POST.get('quantity')

       
        if int(current_inventory) >= int(quantity):
            order_form = OrderForm(data=request.POST)
            if order_form.is_valid():
                order_instance = order_form.save(commit=True)
                order_instance.save()
                ser_order = serializers.serialize('json',[order_instance,])
        

                quantity = request.POST.get('quantity')
                remain_quantity = quantity
                movement_date = datetime.now()
                product = Product.objects.get(id=request.POST['product'])
                order = Order.objects.get(sequence=request.POST['sequence'])
                
                
                source = UserProfileInfo.objects.get(user=request.POST['source'])
                destination = UserProfileInfo.objects.get(user=request.POST['destination'])
                movement_obj = Movement(quantity=quantity, remain_quantity=remain_quantity,
                                        movement_date=movement_date, product=product, order=order,
                                        source=source,destination=destination)   
                movement_obj.save()
                ser_movement = serializers.serialize('json',[movement_obj,])
                current_movements = Movement.objects.filter(destination=source).filter(product=product).exclude(remain_quantity=0).order_by('movement_date')
                movement_obj.assignment(order_quantity=quantity, current_movements=current_movements,movement_obj=movement_obj)
                **order_form.initial['sequence'] = int(Order.objects.order_by('-id')[0].sequence) + 1** 
                return JsonResponse({'ser_order':ser_order, 'ser_movement':ser_movement}, status=200)
            else:
                return JsonResponse({"error": order_form.errors}, status=400)    
        else:
            return HttpResponse('There is Not enough inventory to create an order')   
    elif request.method == 'GET':
        print('******')
        product = request.GET.get('product')
        print(product)
        source = request.GET.get('source')
        print(source)
        current_inventory = Movement.objects.filter(destination=source).filter(product=product).aggregate(Sum('remain_quantity'))['remain_quantity__sum']
        print('------')
        print(current_inventory)
        ser_current_inventory  = {'current_inventory':current_inventory}

        # ser_current_inventory = serializers.serialize('json',[current_inventory,])
        
        return JsonResponse({'ser_current_inventory':ser_current_inventory}, status=200)
    else:
        return JsonResponse({'success':False}, status=400) 

The direct answer to your specific question is that it’s because you’re not saving the model after changing it.

However, doing it this way creates a race condition - there are circumstances where you will get two instances with the same sequence value.

Also:

The second line here is redundant. Specifying commit=True saves the model instance.

If quantity is in your form, this is redundant. (If it’s not, it should be)

This line is redundant based upon the 4th line of your view.

Also redundant: