# setting a value for form field after POST

**URL:** <https://forum.djangoproject.com/t/setting-a-value-for-form-field-after-post/13284>\
**Category:** Django Internals\
**Created:** [April 20, 2022, 9:14pm UTC](https://forum.djangoproject.com/t/setting-a-value-for-form-field-after-post/13284 "2022-04-20T21:14:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bibalani](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bibalani/32/8165_2.png) [@bibalani](https://forum.djangoproject.com/u/bibalani)\
**Post date:** [April 20, 2022, 9:14pm UTC](https://forum.djangoproject.com/t/setting-a-value-for-form-field-after-post/13284/1 "2022-04-20T21:14:41Z")

</div>

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

```auto
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.

```auto
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) 

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 20, 2022, 9:47pm UTC](https://forum.djangoproject.com/t/setting-a-value-for-form-field-after-post/13284/2 "2022-04-20T21:47:46Z")

</div>

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:

> [@bibalani](#):
>
> ```auto
> order_instance = order_form.save(commit=True)
> order_instance.save()
> 
> ```

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

> [@bibalani](#):
>
> ` quantity = request.POST.get('quantity')`

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

> [@bibalani](#):
>
> `product = Product.objects.get(id=request.POST['product'])`

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

Also redundant:

> [@bibalani](#):
>
> `source = UserProfileInfo.objects.get(user=request.POST['source'])`
