info: I am trying to make an app like monthly instalment payment of items(property or etc).
I want when I fill out the customer form and select the product, I have to submit the payment form along with it and that payment will be linked to that customer. So in order to do this work, I have created a CustomerPayment model. I don't know how the models should be designed to do this.
Problem I am trying to submit the customer payment along with the customer form but my form is not submitted it's redirect me to back on form. i don't know how can i pass the both form data as instances of CustomerPayment (customer=customer & payment=payment).
Example:
models.py
class Customer(models.Model):
name = models.CharField(max_length=255)
phone = models.CharField(max_length=11, blank=True)
class Payment(models.Model):
collect_by = models.ForeignKey(User, on_delete=models.SET_NULL)
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL)
datetime = models.DateTimeField(auto_now_add=True)
amount = models.DecimalField(default=0)
remaining = models.DecimalField(default=0)
views.py
def order_form(request):
customer_form = CustomerForm(request.POST)
payment_form = PaymentForm(request.POST)
if request.method == 'POST':
if customer_form.is_valid() and payment_form.is_valid():
customer = customer_form.instance.sales_men = request.user
customer.save(commit=False)
payment = payment_form.instance.collect_by = request.user
payment.instance.customer = customer
payment.save(commit=False)
return redirect('/')
context = {
'customer_form': customer_form,
'payment_form': payment_form,
}
return render(request, "payment/payment_create.html", context)

Paymentand only have 2 models.Paymentmodel FK in Customer model?customerFK from theCustomerPaymentto thePaymentmodel and remove theCustomerPaymentmodel.orderfunction also in views...