如何在表单集验证之前防止模型保存?代码示例:
form = StockMovementForm(data=data)
Formset = inlineformset_factory(StockMovement, StockMovement.products.through, extra=2, can_delete=False)
if form.is_valid():
stock_movement = form.save()
formset = Formset(data=data, instance=stock_movement)
if formset.is_valid():
formset.save()
(StockMovementForm 用于创建新的 *stock_movement*)
我想在检查formset的有效性后保存表单。
我发现了类似的东西:
form = StockMovementForm(data=data)
Formset = inlineformset_factory(StockMovement, StockMovement.products.through, extra=2, can_delete=False)
formset = Formset(data=data)
if form.is_valid():
if formset.is_valid():
stock_movement = form.save()
objects = formset.save(commit=False)
for object in objects:
object.movement = stock_movement
object.save()
但我不知道这是否是一个好方法(在这里找到)。