![](/img/trans.png)
[英]How do I delete a single instance of an intermediate model in a Django Many-to-many (through) relationship?
[英]How to bundle many forms in single instance in django
主要问题是是否有任何方法可以将许多django表单捆绑到单个实例中,以弄清楚我需要解释的问题:
我创建了一堆表单类,这些表单类需要一起工作才能显示单个视图。
from_form = move_forms.WaypointForm(prefix="marker-from", instance=move.from_place)
to_form = move_forms.WaypointForm(prefix="marker-to", instance=move.to_place)
#Notice that last two form are of the same class
through = ThroughFormset(prefix="through", queryset=move.waypoints_db.all())
path_form = move_forms.CarMovePathForm(path=move.path)
date_form = move_forms.MoveForm(instance=move)
#put all this into context instance and render
所有这些形式基本上都显示/编辑相同的数据库实例---但可以在应用程序中以不同的配置重复使用(因此,我不能只手动创建将其封装的类)。
网页中有许多表格是很麻烦的,例如,我必须编写如下代码:
if transportation_form.is_valid() and from_form.is_valid() and \
to_form.is_valid() and through.is_valid() and path_form.is_valid():
我无法将表单属性完全传递给视图,因为大多数viev都以这种方式使用许多表单。
是否有任何明智的方式来捆绑这些表格---还是我的设计坏了(如果可以的话,如何解决)。
那这个呢
from_form = move_forms.WaypointForm(
request.POST or None,
prefix="marker-from",
instance=move.from_place)
# ... other forms declared the same way
forms = {
'from_form': from_form,
'to_form': to_form,
# ...
}
if all(f.is_valid() for f in forms.values()):
# ...
return redirect('success')
return render(request, 'template.html', {'forms': forms})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.