Pete
Pete

Reputation: 13

Compare 2 django model forms

I'm asking for a address twice on a form - billing and delivery. Address is modelled as an object.

How can I compare whether the values of both modelforms are the same - if they are, i'll use the same foreign key from my customer object in both cases.

Upvotes: 1

Views: 199

Answers (1)

Alasdair
Alasdair

Reputation: 308889

You can compare the cleaned_data of both forms.

if request.method == "POST":
    billing_form = AddressForm(prefix="billing", data=request.POST)
    address_form = AddressForm(prefix="delivery", data=request.POST)

    if billing_form.is_valid() and address_form.is_valid():
        if billing_form.cleaned_data == address_form.cleaned_data:
            # addresses are the same
        else:
            # addresses are not the same

From a usability perspective, it's better not to make the customer enter the same address details twice -- give them a tick box e.g. "Use delivery address as billing address?".

Upvotes: 1

Related Questions