I have a Food table which contains Food name and it's price.Whenever a user selects a food from template form,the price associated with it should automatically be displayed in the form. How can I do it ??
models.py
class Food(models.Model):
name = models.CharField(max_length = 100)
price = models.IntegerField()
class Payment(models.Model):
food = models.ForeignKey(Food, on_delete=models.CASCADE)
forms.py
class PaymentForm(forms.ModelForm):
class Meta:
model = Payment
fields = '__all__'
views.py
def payment_details(request):
amounts = Food.objects.all()
context={
'amounts' : amounts
}
return render(request, 'app_name/payment.html',context)
template
<label>Select the Food</label>
<select name="name" class="form-control" required>
{% for amount in amounts %}
<option value="{{amount.food.name}}">{{amount.food.name}}</option>
{% endfor %}
</select>
<label>Amount You Need to Pay</label>
<select name="name" class="form-control" required>
{% for amount in amounts %}
<option value="{{amount.food.price}}">{{amount.food.price}}</option>
{% endfor %}
</select>