2

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:

enter image description here

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)
6
  • 1
    I feel like a customer makes a payment, not multiple customers (that may be wrong) so you could just have the FK to customer on the Payment and only have 2 models. Commented Jul 3, 2021 at 0:06
  • Yeah you are write! i want to get multiple or one payment from one customer. i mean multiple payments along with the single customer. so do you mean i need to add Payment model FK in Customer model? Commented Jul 3, 2021 at 0:17
  • 1
    Move the customer FK from the CustomerPayment to the Payment model and remove the CustomerPayment model. Commented Jul 3, 2021 at 0:18
  • Please take a look i have to update the payment Model and order function also in views... Commented Jul 3, 2021 at 0:32
  • Dear can you tell me how can i pass that customer Instance @markwalker_ Commented Jul 3, 2021 at 0:42

1 Answer 1

1

I think what you want to do in your view is something like this;

def order_form(request):

    customer_form = CustomerForm()
    payment_form = PaymentForm()

    if request.method == 'POST':
        customer_form = CustomerForm(request.POST)
        payment_form = PaymentForm(request.POST)

        if customer_form.is_valid() and payment_form.is_valid():
            customer = customer.save(commit=False)
            customer.sales_men = request.user
            customer.save()

            payment = payment_form.save(commit=False)
            payment.collect_by = request.user
            payment.customer = customer
            payment.save()

            return redirect('/')

    context = {
        'customer_form': customer_form,
        'payment_form': payment_form,
    }
    return render(request, "payment/payment_create.html", context)

If all the Customer model has is a name and phone number, you should consider moving that data on to the Payment. If nothing else, it'd simplify what you're doing in this view.

You've also got a FK on the Customer and a FK on the Payment both pointing to the same User. It feels like this would be simpler if a Payment had a FK to a User.

The User model already holds a name, so you might be able to drop the Customer model, of it there's data still needed make a Profile model which has a user = models.OneToOneField(). Then request.user.profile would give you a user profile for extra data

Sign up to request clarification or add additional context in comments.

2 Comments

Dear it's looks like that customer pay me just one time when i add new customer i can add a one payment from this customer but i want to collect multiple payments from this customer.
Then surely they just fill in the forms again to make another payment?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.