Skip to Main Content
Python Programming Blueprints
book

Python Programming Blueprints

by Daniel Furtado, Marcus Pennington
February 2018
Intermediate to advanced content levelIntermediate to advanced
456 pages
9h 56m
English
Packt Publishing
Content preview from Python Programming Blueprints

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 

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Intelligent Projects Using Python

Intelligent Projects Using Python

Santanu Pattanayak, Manohar Swamynathan
Flask Blueprints

Flask Blueprints

Joël Perras
Matplotlib for Python Developers - Second Edition

Matplotlib for Python Developers - Second Edition

Aldrin Yim, Claire Chung, Allen Yu

Publisher Resources

ISBN: 9781786468161Supplemental Content