Creating the shopping cart form

We now have the models in place. Let's add a new form that will display the cart data on a page for editing. Open the forms.py file at gamestore/main/, and at the end of the file add the following code:

    ShoppingCartFormSet = inlineformset_factory(      ShoppingCart,      ShoppingCartItem,      fields=('quantity', 'price_per_unit'),      extra=0,      widgets={          'quantity': forms.TextInput({             'class': 'form-control quantity',          }),          'price_per_unit': forms.HiddenInput()      }    )

Here, we create an inline formset using the function inlineformset_factory. Inline formsets are suitable when we want to work with related objects via a foreign key. This is very convenient in the case we have here; we have a model ShoppingCart that relates to the 

Get Python Programming Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.