[Answered ]-Django: how to loop through boolean fields and set all to false

1👍

you must return render_to_response('create_order.html', args) one indentation back. so that validation form will not affected. you don’t need to use csrf request if you are using render. I think there is no need to use OrderId in Order model, since it is automattically created. ie id

from django.shortcuts import render
from <appname>.models import Order,Store

def create(request):
    if request.POST:            
        form = OrderForm(request.POST)
        if form.is_valid():
            #i just assume the storeid variable is exist in request.POST and it is ID.
            store_data=Store.objects.get(id=request.POST['storeid'])
            #here you might need to update the other orderform to false
            Order.objects.filter(storeID=store_data).update(Active=False)                
            fo=Order(storeID=store_data,Active=True)
            frm_order=OrderForm(request.POST,instance=fo)                
            frm_order.save()                  
        return HttpResponseRedirect('/liquors/all')
    else:
        form = OrderForm()                  
    return render(request,'create_order.html', {'form':form})

1👍

You can use the order object’s meta object to loop through the fields. Ex.

for field in order._meta.fields():
    if type(field) = django.db.models.fields.BooleanField:
       setattr(order, field.name, True)

Leave a comment