Django, change the value of a form field when I select items in another field

Asked

Viewed 739 times

1

I have a form that registers a sale, I select the customer, the employee (seller), and the items (products) sold and I am placing the total value of the sale manually. What I would like is that when selecting the items, somehow the price of each item is added and the total value automatically played in the sales value field... how can I do this?

# form: 

class VendaForm(forms.ModelForm):
    class Meta:
        model = Venda
        fields = ('cliente', 'func', 'valor', 'itens')

# view:

def efetuar_venda(request):
    if request.method == 'POST':
        form = VendaForm(request.POST)
        if form.is_valid():
            venda = form.save()
            venda.save()
            return redirect('/calcados/', {})
    else:
        form = VendaForm()
        return render(request, 'calcados/cadastrar.html', {'form': form})

# the template cadastrar.html only shows the form given
  • 1

    It’s not very clear how you want to do this, if it’s in the template or in the view. If it is in the template it would be Javascript doubt.

  • No doubt. Javascript/ Jquery would be the solution indicated..

  • obg, with jQuery should look good, but I ended up doing a gambiarra, in the view of adding an item on sale I call the function declared in the sale model that updates the total by adding the price of all items linked to sale using item_set.all() to pick up all items.

  • you can use javascript/jquery, so you would take the values in the template, working on the client side var value = $(id or class..... ). val( ); or $(id or class..... ). date( name );

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.