python
47 lines · 9 steps
Editing an order with inline formsets in Django
A single view edits a parent order and its child line items together, saving both atomically.
Explained by
highlit
1from django.db import transaction
2from django.forms import inlineformset_factory
3from django.shortcuts import get_object_or_404, redirect, render
4
5from .forms import OrderForm, OrderLineItemForm
6from .models import Order, OrderLineItem
7
8LineItemFormSet = inlineformset_factory(
9 Order,
10 OrderLineItem,
11 form=OrderLineItemForm,
12 fields=["product", "quantity", "unit_price"],
13 extra=1,
14 can_delete=True,
15)
16
17
18def edit_order(request, pk):
19 order = get_object_or_404(Order.objects.prefetch_related("line_items"), pk=pk)
20
21 if request.method == "POST":
22 form = OrderForm(request.POST, instance=order)
23 formset = LineItemFormSet(request.POST, instance=order)
24
25 if form.is_valid() and formset.is_valid():
26 with transaction.atomic():
27 order = form.save()
28 line_items = formset.save(commit=False)
29 for item in line_items:
30 item.order = order
31 item.save()
32 for item in formset.deleted_objects:
33 item.delete()
34 order.recalculate_total()
35 return redirect("orders:detail", pk=order.pk)
36 else:
37 form = OrderForm(instance=order)
38 formset = LineItemFormSet(
39 instance=order,
40 initial=[{"unit_price": order.customer.default_price}],
41 )
42
43 return render(
44 request,
45 "orders/edit.html",
46 {"form": form, "formset": formset, "order": order},
47 )
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Inline formsets let one view manage a parent record and its related children in a single form submission.
- 2Wrapping multi-step saves in transaction.atomic keeps parent, children, and derived totals consistent.
- 3commit=False gives you a chance to attach the parent relation before persisting child rows.
Related explainers
python
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status from pydantic import BaseModel, EmailStr from sqlalchemy.orm import Session
Building a signup endpoint in FastAPI
dependency-injection
request-validation
background-tasks
Intermediate
8 steps
php
<?php namespace App\Console\Commands;
Releasing stale document locks in Laravel
artisan-command
transactions
row-locking
Intermediate
6 steps
python
from django import forms from django.utils import timezone from .models import Reservation
Multi-field validation in a Django ModelForm
form validation
cross-field validation
modelform
Intermediate
7 steps
typescript
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable, catchError, concatMap, finalize } from 'rxjs'; import { DataSource, QueryRunner } from 'typeorm';
Wrapping requests in a transaction with NestJS
interceptors
transactions
rxjs
Advanced
7 steps
python
from django import template from django.urls import reverse, NoReverseMatch from django.utils.html import format_html
Active nav-link template tags in Django
template tags
url routing
active state
Intermediate
7 steps
python
from functools import wraps import asyncio from fastapi import APIRouter, FastAPI, Request
Per-route request timeouts in FastAPI
decorators
async
timeouts
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/editing-an-order-with-inline-formsets-in-django-explained-python-d33e/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.