0

I'm building a webshop using Cartridge/Mezzanine. By default however the checkout form (cartridge.shop.forms.OrderForm) contains fields for both first names and last names. I want a form where the customer just has to provide a name. Further, I don't want to use the fields for Country and State/Region.

So far I've created a CustomOrderForm that subclasses the OrderForm and added a 'name'-field. I also attempted to override the init method using this:

class CustomOrderForm(OrderForm):
    name = forms.CharField(label=("Navn"))

    def __init__(self, *args, **kwargs):
        super(OrderForm, self).__init__(*args, **kwargs)
        self.fields.pop('billing_detail_country')

This doesn't really do me any good except the 'name'-field is displayed in the template. But I still need to take the input from this field, split it into first name and last name and populate the relevant fields with it.

How do I do this?

andreashhp
  • 485
  • 2
  • 16
  • If you need first name/last name you probably want to make those separate fields. If you're just splitting on a space character, what happens when there is more than one space in the submitted name? – Ryne Everett Apr 22 '16 at 01:04

1 Answers1

0

This is how I removed the fields Country and State/Regions from my Order Form:-

class CustomOrderForm(OrderForm):
    class Meta:
        model = Order
        fields = ([f.name for f in Order._meta.fields if
                   f.name.startswith("billing_detail") or
                   f.name.startswith("shipping_detail")
                   ] 
                    +
                   ["additional_instructions", "discount_code"])
    def __init__(
        self, request, step, data=None, initial=None, errors=None,
        **kwargs):
        res = super(CustomOrderForm, self).__init__(request, step, data, initial, errors, **kwargs)
        del self.fields["billing_detail_country"]
        del self.fields["shipping_detail_country"]
        return res

Since it's a dictionary I used the del method. Hope so this is useful as you are being answered after such a long time.

Shashishekhar Hasabnis
  • 1,636
  • 1
  • 15
  • 36