jeudi 7 décembre 2017

What would be the robust design pattern for multiple class forms render/process (create, update) on single page in django

I am working on a django project in which I have to design a sales order page for order processing,now as per requirement I have to create multiple information forms on single page with different fields (formset does not helpful in this case) which I achieved by creating specific form class and then render them in template through class view function

def get_context_data(self, **kwargs):
    context = super(CreateOrderView, self).get_context_data(**kwargs)
    context['ord_info_form'] = OrderInformationForm()
    context['billing_form'] = BillingInformationForm(prefix='billing')
    context['shipping_form'] = ShipingInformationForm(prefix='shipping')
    context['payment_form'] = PaymentInfoForm()
    context['summary_form'] = OrderProductsForm()
    return context

Now, I have to save data for all forms which belongs to multiple models like billing, contact and shipping information. So for save information I created another form class for create order and set it in form_class variable in Createview class, and I override the save function in form class in which I manipulate the data for all model forms. For update order (put operation) I did the same I create another form class for update like create one in form class.

As per my understanding I implement this strategy, but this seems to me repetitive design. How can I design it better for multiple forms with different fields. Please let me know what kind of design pattern I can apply for this.

P.S: If require I'll post more code and assets for clear understanding.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire